Set Interface in Java is an interface of the Collection framework present since JDK 1.2.
Important Points About Set Interface
- Set interface maintains the uniqueness by not storing duplicate data.
- It is not an index-based.
- There are 3 implementation classes of Set Interface.
When do we go for Set?
We go for SET when we want to represent a group of individual objects as a single entity where duplicates are not allowed or insertion order is not preserved.
Note: Set interface does not have any methods of its own.
Implementation Class of Set Interface
- HashSet
- LinkedHashSet
- TreeSet
HashSet
HashSet is the implementation class of Set interface.
Important Points of HashSet
- The underlying data structure is Hashtable
- It does not allow duplicate.
- It does not preserve the insertion order.
- It stores objects based on their hashcode.
- It allows for NULL insertion.
- It allows for heterogeneous objects.
- It implements both Serializable and Cloneable interface but not RandomAccess
- It is the best choice when our frequent operation is retrieval.
Overloaded Constructor
- HashSet(int initialCapacity): It creates an empty HashSet object with specified initial capacity and default fill ratio or load factor as 0.75
- HashSet(int initialCapacity, float load factor): It creates an empty HashSet object with the specified initial capacity and specified fill ratio or load factor.
- HashSet(Collection c) → It creates an equivalent HashSet object from any given collection object.
LinkedHashset
LinkedHashset is the child class of the HashSet interface.
Important Point of LinkedHashSet
- The underlying data structure is Hashtable and LinkedList.
- It does not allow duplicate.
- It preserves insertion order.
- It allows for NULL insertion.
- It allows for heterogeneous objects.
- It implements both Serializable and Cloneable interface but not RandomAccess
When should we go for LinkedHashSet?
When we want to represent a group of individual objects as a single entity without allowing duplicate and preserving the insertion order, then we should go for LinkedHashSet.
This is mainly used to develop a cache-based application.
SortedSet
This is the child interface of the Set interface.
Important Points About SortedSet
- It does not allow duplicates.
- It does not allow heterogeneous objects.
- It stores the objects based on some sorting order
Few Methods of SortedSet interface :
method | description | Return Type |
---|---|---|
first() | It returns the first object present in the given collection object. | Object |
last() | It returns the last object present in the given collection object. | Object |
headset(Object obj ) | It returns a sorted object with elements < obj | SortedSet |
tailSet(Object obj) | It returns a sorted object with elements >= obj | SortedSet |
subset(Object obj1, Object obj2) | It returns a sorted object with elements >= obj1 and < obj2 | SortedSet |
comparator() | It returns an instance of Comparator object in case of custom sort. If it is default sorting, it returns NULL. For integer values, the default natural sorting is ascending order → For string values, the default natural sorting in alphabetical order. | Comparator |
iterator() | In order to get a single element at a time from the collection. | Iterator |
Note: These above methods must be called on an implementation class object of the SortedSet interface.
TreeSet
It is the implementation class of the SortedSet interface.
Important Points Of TreeSet
- The underlying data structure is a Balanced Tree.
- It does not allow duplicate.
- Insertion order is not preserved but elements will be stored based on some sorting order.
- It can store only one NULL.
- It does not allow heterogeneous objects.
When do you get ClassCastException in TreeSet?
TreeSet does not allow a heterogeneous object, if we still try to add heterogeneous objects, we get ClassCastException.
Can you add NULL into a TreeSet object? OR What is NULL acceptance in the TreeSet object?
In the case of an empty TreeSet object, we can insert only one NULL as the first element. After inserting NULL as the first element, if we try to insert any other element, then we get NullPointerException.
Whereas, in the case of nonempty TreeSet object, we can’t insert NULL. If we try to do so, we get NullPointerException.
Constructors of TreeSet :
- TreeSet ts = new TreeSet() : It is used to create an empty Treeset object with default natural sorting order.
- TreeSet ts = new TreeSet(Comparator c): It is used to create an empty Treeset object with a custom sorting order.
- TreeSet ts = new TreeSet(Collection c)
- TreeSet ts = new TreeSet(SortedSet s): Default Sorting in TreeSet :
Program example :
What are the preconditions for default sorting?
The object should be both comparable and homogeneous.
Note: If these 2 preconditions are not satisfied, then we get ClassCastException.
Thanks a lot for reading my article. It’s highly appreciated. I hope you must learn something new from here.
And if you have any suggestions regarding my articles Please let me know. If you want to learn some other concept of Java, please check out the articles list in the Right Corner of your screen.