Set is child interface of Collection interface
Where we should use Set?
If you want to represent group of object where duplication not allowed and insertion order is not preserved.
Set interface do not define any new method and all the collection methods are applicable to set
Implementation class:
HashSet
- Underlying data structure that HashSet follows is Hashtable
- Duplicates are not allowed
- Insertion order is not preserved(But all object are inserted based on hashcode of Objects)
- Can contain hetrogenious object and Homogenios Objects
- Can contain null value
- Implements Serialization and Cloanable intreface
- If our operation is searching then HashSet is best choice
Answer is no error because public boolean add() method return boolean as we can see so if we try to add duplicate element it will just return false value.
Constructor
- HashSet hs = new HashSet();
- Create empty HashSet with default initial capacity is 16 and with fill ratio 0.75
- HashSet hs = new HashSet(int initialCapacity);
- If we know the no. of element we want to insert into HashSet then we can use this constructor but the load factor is 0.75 only.
- HashSet hs = new HashSet(int initialCapacity,float loadFactor);
- If we want to set user defined load factor which define that after filling this much of HashSet a new HashSet is to be created
- HashSet hs = new HashSet(Collection c);
- If we want to convert any collection object to HashSet
public static void main(String...s){
HashSet hs = new HashSet();
hs.add("A");
hs.add("B");
hs.add(1);
hs.add(null);
System.out.println(hs.add("A"));//false
System.out.println(hs);//[null,B,A,1]
}
}
Linked HashSet
- Child class of HashSet
- Where is duplication is not allowed
- Insertion order is preserved
HashSet
|
Linked
HashSet
|
Underlying
data structure is Hashtable
|
Underlying data structure is Linked list +Hashtable
|
Insertion
order is not preserved
|
Insertion order is preserved
|
Came in 1.2
version
|
Came in 1.4 version
|
class LinkedHashSetExample{
public static void main(String...s){
LinkedHashSet lhs = new LinkedHashSet();
lhs.add("A");
lhs.add("B");
lhs.add(1);
lhs.add(null);
System.out.println(lhs.add("A"));//false
System.out.println(lhs);//[A,B,1,null]
}
}
Recollect what is the output when we used Hashset which was [null,B,A,1] no insertion order is reserved but using LinkedHshSet we [A,B,1,null] in insertion orderExample of LinkedHashSet assume of cache memory where we do not need Duplication but Insertion order is preserved so Linked HashSet is the best choice
SortedSet
Used when you don't want duplicates but added in some order
example can you please represent a group of student according to their roll no. in ascending order.
Method of Sortedset
There are some specific methods which are applicable to only SortedSet
- public Object getFirst();
- public Object getLast();
- public SortedSet headSet(Object o); less then the Object specified
- public SortedSet tailSet(Object o); greater then the object specified and equal to Object o
- public SortedSet subSet(Object o1 ,Object o2);
- public Comparator caparator(); Specifying some sorting order
Default natural sorting order for
- Number: Ascending order
- String: Alphabetic order
Example {1,2,3,5,6,7,8,9}
TreeSet:- first(); -> 1
- last(); -> 9
- headSet(5); -> {1,2,3}
- tailSet(5); -> {5,6,7,8,9}
- subSet(2,7); -> {2,3,5,6}
- comparator();-> null
- Underlying data structure Balanced Tree
- Insertion order is not preserved but elements are added in some order
- Duplication is not allowed
- Heterogeneous object are not allowed if we are trying to insert heterogeneous object than at runtime there will be a exception ClassCastException
- null can be inserted but only once
- TreeSet ts = new TreeSet();
- Create empty TreeSet but with default natural sorting order
- TreeSet ts = new TreeSet(Comparator c);
- When you want elements to be inserted by some customized order
- TreeSet ts = new TreeSet(Collection c);
- To create TreeSet of any collection Object
- TreeSet ts = new TreeSet(SortedSet s);
- For any SortedSet we can have TreeSet object
public static void main(String...s){
TreeSet ts = new TreeSet();
ts.add("A");
ts.add("B");
ts.add("a");
ts.add("Z");
System.out.println(ts);//[A,B,Z,a]
}
}
Elements are stored in default sorting Order as we have used first constructor
1 .Try to insert Heterogeneous object
Elements are stored in default sorting Order as we have used first constructor
1 .Try to insert Heterogeneous object
class TreeSetExample{
public static void main(String...s){
TreeSet ts = new TreeSet();
ts.add("A");
ts.add("B");
ts.add("a");
ts.add("Z");
ts.add("1"); // ClassCastException
ts.add("1"); // ClassCastException
System.out.println(ts);//[A,B,Z,a]
}
}
ClassCastException as we are trying to add Heterogeneous objects
2. For non-empty TreeSet then we will get NullPointerException because to add null in between then elements is going to comparision phase as elements are stored in some sorting order
class TreeSetExample{
public static void main(String...s){
TreeSet ts = new TreeSet();
ts.add("A");
ts.add("B");
ts.add("a");
ts.add("Z");
ts.add(null); // NullPointerException
ts.add(null); // NullPointerException
System.out.println(ts);//[A,B,Z,a]
}
}
3. For the first element of TreeSet null insertion is possible but after null as first element and we try to add any element again NullPointerException
class TreeSetExample{
public static void main(String...s){
TreeSet ts = new TreeSet();
ts.add(null); //First Element
}
}
class TreeSetExample{
public static void main(String...s){
TreeSet ts = new TreeSet();
ts.add(null);
ts.add("A");// NullPointerException
}
}
Example:
class TreeSetExample{
public static void main(String...s){
TreeSet ts = new TreeSet();
ts.add(new StringBuffer("A"));
ts.add(new StringBuffer("B"));
ts.add(new StringBuffer("Z"));
ts.add(new StringBuffer("L"));
System.out.println(ts);
System.out.println(ts);
}
}
Now what will be the Output is it {A,B,L,Z} or {Z,L,B,A}?
this code will be compiled but at runtime there will be Exception says ClassCastException why?
we are storing Homogeneous object so why exception,so if are storing object in default natural order then Object should be:
- Homogeneous Object
- Is the Object of Comparable
And StringBuffer is not Object of Comparable mean class don't implements Comparable


0 comments :
Post a Comment