Set interface:
1. Set Interface in Java is present in java.util package. It extends the Collection interface.
2. If we want to represent group of individual objects as a single entity where duplicates are not allows and insertion order not preserved.
3. We can store at most one null value in Set.
4. Set interface doesn’t contain any new method and we have to use only Collection interface methods.
HashSet:
- HashSet is one of the implementation class of Set interface present Since JDK 1.2.
- HashSet implements data Structure called Hashtable.
- HashSet doesn’t allow duplicate data.
- Insertion order is not preserved and Objects are inserted based on hash code.
- Heterogeneous objects are allowed.
- Accepts Single null element.
- Implements Cloneable, Serializable marker interface but not RandomAccess.
- HashSet is the best choice if the frequent operation is search operation. Search operation works based on hash code.
- Initial capacity is 16 and Fill ratio or load factor is 75%.
Note: In HashSet duplicates are not allowed if we are trying to insert duplicates then we won’t get any compile time or runtime errors and add() method simply returns false.
There are 4 overloaded constructors
creates an empty HashSet object with specified initial capacity and default fill ratio 0.75.
creates an empty HashSet object with specified initial capacity and customized fill ratio.
creates an equivalent HashSet for the given Collection.
This Constructor meant for inter conversion between Collection objects
FillRatio / LoadFactor:
After filling how much ration a new HashSet object will be created, this ratio is called FillRatio / LoadFactor.
For example, FillRatio 0.75 means, after filling 75% ratio, a new HashSet object will be created.
Output:
false
[Java, Hibernate, null, JDBC, Spring, Servlets]
LinkedHashSet:
- LinkedHashSet is the child class of HashSet interface present since JDK 1.4.
- LinkedHashSet is just like HashSet but it maintains insertion order.
- It is exactly same as HashSet(including constructors) except underlined data structure.
- HashSet implements data Structure called LinkedList + Hashtable.
Output:
false
[Java, Servlets, JDBC, Spring, null, Hibernate]
Note: In general, we can use LinkedHashSet to develop cache-based applications where duplicates are not allowed and insertion order preserved.
SortedSet interface:
- SortedSet is the child interface of Set.
- If we want to represent a group of individual objects according to some sorting order without duplicates then we should go for SortedSet.
SortedSet interface defines the following specific methods:
returns SortedSet whose elements are >= obj1 and < obj2.
returns Comparator object that describes underlying sorting technique. If we are using default natural sorting order then we will get null.
Note: The default natural sorting order is Ascending order and for String objects is Alphabetical order.
For Example
{100, 101, 104, 106, 110, 115, 120}
first() ----> 100
last() ----> 120
headSet() ----> [100, 101, 104]
tailSet() ----> [106, 110, 115, 120]
subSet(101, 115) ----> [101, 104, 106, 110]
TreeSet:
- TreeSet is one of the implementation class of Set interface and present since JDK 1.2.
- TreeSet is not a hash-based collection, the data structure used is Balanced tree.
- TreeSet is mainly used for uniqueness and sorting. i.e.., TreeSet doesn’t store duplicate data and also
- Implements default natural sorting order.
- Insertion order not preserved.
- Heterogeneous objects are not allowed otherwise we will get runtime exception saying ClassCastException.
- TreeSet is Homogeneous i.e.., TreeSet can store only one type of data.
- All objects will be inserted on based on some sorting order it may be default natural sorting order or customized sorting order.
- TreeSet implements Serializable, Cloneable but not RandomAccess
- TreeSet cannot store even single null element.
- TreeSet either uses Comparable or Comparator interface.
Note:
Comparable is used for default natural sorting.
Comparator is used for Custom sorting.
Constructors:
creates an empty TreeSet object where the elements will be inserted according to default natural sorting order.
creates an empty TreeSet object where the elements will be inserted to according to customised sorting order specified by Comparator object
Output:
[NTR, PK, Prabhas, RC, mb]
Null acceptance:
- For non-empty TreeSet we are trying to insert null then we will get NullPointerException.
- For empty TreeSet first element as null is allowed. But after inserting that null if we are trying to insert other then we will get runtime exception saying NullPointerException
Note: Until 1.6V null is allows as first element to the empty TreeSet but from 1.7V onwards null is not allowed even as the first element that is "Null" such type of element is not applicable from 1.7V.
RE: ClassCastException
If we are depending on default natural sorting compulsory the objects should be Homogeneous and Comparable otherwise, we will get runtime exception saying ClassCastException.
An Object is said to be Comparable if and only if the corresponding class implements Comparable interface.
String class and wrapper classes already implemented Comparable interface but StringBuffer class doesn’t implement Comparable interface. Hence, we got ClassCastException in the above example.
Comparison table of Set implemented classes:
Property | HashSet | LinkedHashSet | TreeSet |
---|---|---|---|
Underlined Data structure | Hashtable | Combination of LinkedList + Hashtable | Balanced tree |
Duplicate objects | Not allowed | Not allowed | Not allowed |
Insertion order | Not preserved | Preserved | Not preserved |
Sorting order | Not applicable | Not applicable | Applicable |
Heterogeneous Objects | Allowed | Allowed | Not allowed |
Null acceptance | Allowed | Allowed | For empty TreeSet as first element null is allowed but this rule is allowed until 1.6V only. From 1.7V null is not allowed even in first element. |
As a part of 1.6 version one of the concept introduced in Collection framework is NavigableSet. For more information on NavigableSet go through to Map Interface.
0 Comments