September 26, 2021

HashSet, LinkedHashSet, TreeSet examples

What is Set? Will go with the mathematical explanation: A set is a collection of well defined and distinct objects. So HashSet, LinkedHashSet, TreeSet will save distinct objects.

HashSet: It saves distinct objects/elements. But it doesn’t guarantee about the order while retrieving. In this example I have used Iterator object to iterate the HashSet.

package com.chetasmind.setPackage;

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

public class SetExamples {

	public static void main(String[] args) {

		HashSet<String>hashSet = new HashSet<>();
		hashSet.add("red");
		hashSet.add("green");
		hashSet.add("orange");
		hashSet.add(null);
		hashSet.add("red");
		hashSet.add("blue");
		hashSet.add("green");
		
		Iterator<String>itr = hashSet.iterator();
		String str="";
		
		System.out.println("\n Display content of HashSet object");
		while(itr.hasNext()) {
			str = itr.next(); //In the while loop, do not write itr.next() multiple times.
			System.out.println(str);
		}	
	}
}

LinkedHashSet: LinkedHashSet works like HashSet, but it maintains the insertion order. Add below code after the while loop.

LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("red");
linkedHashSet.add("green");
linkedHashSet.add("orange");
linkedHashSet.add("red");
linkedHashSet.add("blue");
linkedHashSet.add("green");
		
itr = linkedHashSet.iterator();
System.out.println("\n Display content of LinkedHashSet object.");
		
while(itr.hasNext()) {
      str = itr.next();
      System.out.println(str);
}

TreeSet: The elements are ordered using their natural ordering, or by a Comparator provided at set creation time.

TreeSet<String>treeSet = new TreeSet<>();
treeSet.add("red");
treeSet.add("green");
treeSet.add("orange");
treeSet.add("red");
//treeSet.add(null);
treeSet.add("blue");
treeSet.add("green");
		
itr = treeSet.iterator();
System.out.println("\n Display content of TreeSet object");
		
while(itr.hasNext()) {
    str = itr.next();
    System.out.println(str);
}

The above application is also present in my github repository.
If you are writing java applications for the first time, refer my Project structure article.

Leave a Reply

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