HashSet v/s LinkedHashSet v/s TreeSet

In this article, we will compare important implementation classes of Set i.e.; HashSet v/s LinkedHashSet v/s TreeSet

So let’s us discuss in tabular format;

HashSet v/s LinkedHashSet v/s TreeSet:

 HashSet LinkedHashSet TreeSet
Uses hashtable to store element/objects where duplicate element/objects are NOT allowed Uses combination of (hashtable + LinkedList) to store element/objects where duplicate element/objects are NOT allowed Uses balanced-tree to store element/objects where duplicate element/objects are NOT allowed
Insertion-order is NOT maintained, as it uses hashing technique to store element/objects Insertion-order is maintained, as it uses doubly-linked list to store element/objects Insertion-order is NOT maintained, as element/objects are stored according to some sorting-order
HashSet doesn’t deal with  sorting-order;

but it can be converted to TreeSet using inter-conversion constructor, which sorts element/objects in sorting-order

TreeSet ts = new TreeSet(hashSet);

LinkedHashSet doesn’t deal with  sorting-order;

but it can be converted to TreeSet using inter-conversion constructor, which sorts element/objects in sorting-order

TreeSet ts = new TreeSet(linkedHashSet);

Element/objects stored in TreeSet are according to some sorting-order;

it could be either default natural sorting-order or programmer defined customized sorting-order

While iterating HashSet, we will get items in random-order While iterating LinkedHashSet, we will get items as per insertion-order While iterating TreeSet, we will get items in sorted-order;

either natural-ordering or customized sorting-order

This is introduced in original collection framework in Java 1.2 version This is introduced in Java 1.4 version This is also introduced in original collection framework in Java 1.2 version
Allows NULL insertion but maximum of only one NULL value Allows NULL insertion but maximum of only one NULL value From Java 1.7 version, NULL is not allowed to insert;

Till Java version 1.6, only one NULL is allowed that too as 1st element

14-Set-interace-in-java

Set Program using HashSet, LinkedHashSet and TreeSet :

SetExample.java

package in.bench.resources.collection;

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

public class SetExample {

	public static void main(String[] args) {

		// 1. Creating HashSet object
		Set<String> hashSet = new HashSet<String>();

		// add few elements
		hashSet.add("Vijay");
		hashSet.add("Ajith");
		hashSet.add("Vikram");
		hashSet.add("Suriya");

		System.out.println("Displaying HashSet elements"
				+ " in Random-order : \n");
		for(String actor : hashSet) {
			System.out.println(actor);
		}

		// 2. Creating HashSet object
		Set<String> linkedHashSet = new LinkedHashSet<String>();

		// add few elements
		linkedHashSet.add("Sharukh");
		linkedHashSet.add("Akshay");
		linkedHashSet.add("Saif");
		linkedHashSet.add("Salman");

		System.out.println("\nDisplaying LinkedHashSet elements"
				+ " as per Insertion-order : \n");
		for(String actor : linkedHashSet) {
			System.out.println(actor);
		}

		// 3. Creating HashSet object
		Set<String> treeSet = new TreeSet<String>();

		// add few elements
		treeSet.add("Kareena");
		treeSet.add("Priyanka");
		treeSet.add("Deepika");
		treeSet.add("Anushka");

		System.out.println("\nDisplaying TreeSet elements"
				+ " as per ASC Sorting-order : \n");
		for(String actor : treeSet) {
			System.out.println(actor);
		}
	}
}

Output:

Displaying HashSet elements in Random-order : 

Ajith
Vijay
Suriya
Vikram

Displaying LinkedHashSet elements as per Insertion-order : 

Sharukh
Akshay
Saif
Salman

Displaying TreeSet elements as per ASC Sorting-order : 

Anushka
Deepika
Kareena
Priyanka

 

Factors to consider while discussing any collection class

We should consider below factors while discussing any implementation class of collection framework or for that matter Map interface,

  • Underlying data structure
  • Duplicates are allowed or Not
  • Insertion order is maintained or Not
  • Whether NULL insertion is possible or Not
  • If possible, how many NULL values can be inserted
  • Whether collection class provide sorting, by default
  • Is there any way to apply customized sorting
  • Performance, while dealing with retrieval or manipulation (addition/deletion)
  • By default, all methods are synchronized or Not

 

References:

 

Happy Coding !!
Happy Learning !!

Comparable interface
List v/s Set