Saturday, 13 January 2018

What are classes implementing Set interface?

We have three classes implementing set interface in java Collection framework hierarchy. Look at the diagram given below:

set interface java radar


From the diagram its clear that classes implementing set interface are:

1. Hashset

HashSet implements Set interface and extends AbstractSet class. It is part of java.util package.
HashSet contain unique elements only. If you add duplicate in it, previous value will be overwritten.
HashSet allows one null value only.
HashSet doesn’t maintain insertion order so when you retrieve elements from it they may be returned in random order. If you want to get element in order of insertion, use LinkedHashSet.

For more details about Treeset follow link Hashset in java


2. LinkedHashset

LinkedHashSet implements Set interface and extends HashSet class. It is part of java.util package.

LinkedHashSet is almost similar to HashSet except that it maintains the insertion order of the elements.

For more details about Treeset follow link LinkedHashset in java


3. Treeset

Treeset class implements set interface and extends AbstractSet class.
When storing element in treeset, it stores them in ascending order by default.

For more details about Treeset follow link Treeset in java

No comments:

Post a Comment

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.