PUB ID

Monday, 30 November 2015

Difference between hashset and treeset and linkedhashset in java

TreeSet, LinkedHashSet and HashSet all are implementation of Set interface. there are some differences exist between them.



HashSet
LinkedHashSet
TreeSet
How they work internally?
HashSet uses HashMap internally to store it’s elements.
LinkedHashSet uses  LinkedHashMap internally to store it’s elements.
TreeSet uses TreeMap internally to store it’s elements.
Order Of Elements
HashSet doesn’t maintain any order of elements.
LinkedHashSet maintains insertion order of elements. i.e elements are placed as they are inserted.
TreeSet orders the elements according to supplied Comparator. If no comparator is supplied, elements will be placed in their natural ascending order.
Performance
HashSet gives better performance than the LinkedHashSet and TreeSet.
The performance of LinkedHashSetis between HashSet and TreeSet. It’s performance is almost similar to HashSet. But slightly in the slower side as it also maintains LinkedList internally to maintain the insertion order of elements.

TreeSet gives less performance than the HashSet and LinkedHashSet as it has to sort the elements after each insertion and removal operations.
Insertion, Removal And Retrieval Operation
HashSetgives  performance of order O(1) for insertion, removal and retrieval operations.

LinkedHashSet also gives performance of order O(1) for insertion, removal and retrieval operations.
TreeSet gives performance of order O(log(n)) for insertion, removal and retrieval operations
Null elements
HashSet allows maximum one null element.
LinkedHashSet also allows maximum one null element.
TreeSet doesn’t allow even a single null element. If you try to insert null element into TreeSet, it throws NullPointerException.
Memory Occupation
HashSet requires less memory than LinkedHashSet and TreeSet as it uses only HashMap internally to store its elements.
LinkedHashSet requires more memory than HashSet as it also maintains LinkedList along with HashMap to store its elements.
TreeSet also requires more memory than HashSet as it also maintains Comparator to sort the elements along with the TreeMap.
When To Use?
Use HashSet if you don’t want to maintain any order of elements.
Use LinkedHashSet if you want to maintain insertion order of elements.
.
Use TreeSet if you want to sort the elements according to some Comparator


 
HashSet Example
 
HashSet<Dog> dset = new HashSet<Dog>();
dset.add(new Dog(2));
dset.add(new Dog(1));
dset.add(new Dog(3));
dset.add(new Dog(5));
dset.add(new Dog(4));
Iterator<Dog> iterator = dset.iterator();
while (iterator.hasNext()) {
 System.out.print(iterator.next() + " ");
}
 
Output:
5 3 2 1 4
 
LinkedHashSet Example

LinkedHashSet<Dog> dset = new LinkedHashSet<Dog>();
dset.add(new Dog(2));
dset.add(new Dog(1));
dset.add(new Dog(3));
dset.add(new Dog(5));
dset.add(new Dog(4));
Iterator<Dog> iterator = dset.iterator();
while (iterator.hasNext()) {
 System.out.print(iterator.next() + " ");
}
 
The order of the output is certain and it is the insertion order:
2 1 3 5 4
 
 TreeSet Example

TreeSet<Integer> tree = new TreeSet<Integer>();
tree.add(12);
tree.add(63);
tree.add(34);
tree.add(45);
 
Iterator<Integer> iterator = tree.iterator();
System.out.print("Tree set data: ");
while (iterator.hasNext()) {
    System.out.print(iterator.next() + " ");
}

Output is sorted as follows:
Tree set data: 12 34 45 63 


No comments:

Post a Comment