Learn Set in Java.
Learn Set in Java
Hello Readers,
Today we will see what is Set in Java.
Set is nothing but a interface which is a part of Collection framework and utils package.
Set Implement Collection interface and implemented few important classes which we will see.
- Set has below implementation :
- HashSet
- HashSet does not preserve insertion order istead it preserves sorting order.
- HashSet have default capacity of 16 and has load factor of 75% which means if hashset get filled up to 75% then its capacity will get increased.
- we also provide capacity and load factor inside its constructor parameter.
- we can pass any collection object to hashset constructor.
- It doesn't allow duplicate values.
- it allow different type of objects to add.
- Hashset have one child class which is LinkedHAshSet.
- It preserve insertion order.
- HashTable and Doubly Linked List are used.
- its default capacity is 16 and other cosntructors are like hashset constructor.
- it uses doubly linked list and hashtable for formation of linked hash set.
- LinkedHashSet:-
- It is very useful implementation which help us in many way.
- It preserve insertion order which is not happen in HashSet.
- It allow 1 null value.
- As it implements Set interface it obviously takes only unique element.
- SortedSet:-
- It is a another type of Set implemenation class.
- It implement set interface.
- In this set duplicates are not allowed.
- It preserve sorting order; it store values in sorted order.
- NavigableSet:-
- It is a child interface which is implemented by TreeSet.
- It have some addtional method to navigate the set.
- TreeSet:-
- Its a implementation class of Navigable set.
- It preserve natural sorting order .
- It does not allow homogeneous objects means only same type of objects are allowed.
- It does not allow null values in it.
- Duplicate values are not allowed.
- there are 4 constructor:
- TreeSet()
- TreeSet(Comparator c)
- for custom sorting
- TreeSet(Collection c)
- we can pass any collection object in it.
- TreeSet(SortedSet s)
- Default natural sorting is applicable for String and integer data types only. reason behind it is compareTo() function of comparable interface is implemented by string, integer and wrapper classes.
- Those who not implemented that comparable interface will not get natural sorting.
This was all about Set interface I hope it will help you increase your knowledge and understand easily.
Comments
Post a Comment