Hashset Linkedhashset Treeset difference

Hashset Vs Linkedhashset Vs Treeset:

HashSet, LinkedHashSet, TreeSet:
A set is a collection that does not allow duplicates. So HashSet, LinkedHashSet and TreeSets are does not allow the duplicate elements.

HashSet:
Element order will not be guaranteed.

LinkedHashSet:
Elements are ordered as same like the insertion order.

TreeSet:
Elements are naturally ordered.

It does not allow null.

HashSet, TreeSet and LinkedHashSet Example program:


import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class SetTypes {

public static void main(String[] args) {
 Set<String> myHashSet = new HashSet<String>();
 Set<String> myTreeSet = new TreeSet<String>();
 Set<String> myLinkedHashSet = new LinkedHashSet<String>();

 myHashSet.add("Jan");
 myHashSet.add("Feb");
 myHashSet.add("Mar");
 myHashSet.add("Apr");
 myHashSet.add("Feb");

 myTreeSet.add("Jan");
 myTreeSet.add("Feb");
 myTreeSet.add("Mar");
 myTreeSet.add("Apr");
 myTreeSet.add("Jan");

 myLinkedHashSet.add("Jan");
 myLinkedHashSet.add("Feb");
 myLinkedHashSet.add("Mar");
 myLinkedHashSet.add("Apr");
 myLinkedHashSet.add("Jan");

 Iterator hashSetIterator = myHashSet.iterator();
 while(hashSetIterator.hasNext()){
 // No guarantee for ordering.
 System.out.println("Hashset is"+hashSetIterator.next());
 }

 Iterator treeSetIterator = myTreeSet.iterator();
 while(treeSetIterator.hasNext()){
 //Natural Ordered elemetns
 System.out.println("Treeset is"+treeSetIterator.next());
 }

 Iterator linkedHashSetIterator = myLinkedHashSet.iterator();
 while(linkedHashSetIterator.hasNext()){
 //Ordered elements as we have inserted
 System.out.println("Linkedset is"+linkedHashSetIterator.next());
 }

 }

}

Output:

Hashset isFeb
Hashset isMar
Hashset isApr
Hashset isJan
Treeset isApr
Treeset isFeb
Treeset isJan
Treeset isMar
Linkedset isJan
Linkedset isFeb
Linkedset isMar
Linkedset isApr

Another Example program:

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;

public class HashSetvsVsLinkedHashSetVsTreeSet {

      public static void main(String[] args) {
            Set<String> myHashSet = new HashSet<String>();
            myHashSet.add("Chennai");
            myHashSet.add("Bangalore");
            myHashSet.add("Chennai");
            myHashSet.add("Mumbai");
            myHashSet.add("Delhi");
            Iterator iterHashSet = myHashSet.iterator();
            System.out.println("\n HashSet values:\n");
            while(iterHashSet.hasNext()){
                  System.out.println(iterHashSet.next());
            }

            Set<String> myTreeSet = new TreeSet<String>();
            myTreeSet.add("Chennai");
            myTreeSet.add("Bangalore");
            myTreeSet.add("Chennai");
            myTreeSet.add("Mumbai");
            myTreeSet.add("Delhi");

            Iterator iterTreeSet = myTreeSet.iterator();

            System.out.println("\n TreeSet values:\n");
            while(iterTreeSet.hasNext()){
                  System.out.println(iterTreeSet.next());
            }

            Set<String> myLinkedHashSet = new LinkedHashSet<String>();
            myLinkedHashSet.add("Chennai");
            myLinkedHashSet.add("Bangalore");
            myLinkedHashSet.add("Chennai");
            myLinkedHashSet.add("Mumbai");
            myLinkedHashSet.add("Delhi");

            System.out.println("\n LinkedHashSet values:\n");
            Iterator iterLinkedHashSet = myLinkedHashSet.iterator();
            while(iterLinkedHashSet.hasNext()){
                  System.out.println(iterLinkedHashSet.next());
            }
      }

}

Output:
HashSet values:

Mumbai
Bangalore
Chennai
Delhi

TreeSet values:

Bangalore
Chennai
Delhi
Mumbai

LinkedHashSet values:

Chennai
Bangalore
Mumbai
Delhi

748 total views, 3 views today

Leave a Reply

Your email address will not be published. Required fields are marked *