What is LinkedHashSet collection in java (class hierarchy & example)

  1. Hashtable and linked list implementation of the Set interface, with predictable iteration order.
  2. LinkedHashSet maintains the Insertion order of elements using LinkedList
  3. LinkedHashSet is UnSynchronized and not thread safe.
  4. Iterator of LinkedHashSet is fail-fast.
    • Iterator will throw ConcurrentModificationException, if LinkedHashSet modified at any time after the iterator is created, in any way except through the iterator’s own remove method.
  5. LinkedHashSet offers constant time performance for the basic operations like add, remove, contains and size.

1. LinkedHashSet class hierarchy:

LinkedHashSet collection java class hierarchy
Fig 1: LinkedHashSet class hierarchy

2. Program – Example of LinkedHashSet collection in java (example)

package org.learn.collection.set.lhset;

import java.util.LinkedHashSet;
import java.util.Set;

public class DemoLinkedHashSet {

	public static void main(String[] args) {
		Set<String> computerGenerations = new LinkedHashSet<>();
		computerGenerations.add("VacuumTubes");
		computerGenerations.add("Transistors");
		computerGenerations.add("IntegratedCircuits");
		computerGenerations.add("Microprocessors");
		computerGenerations.add("ArtificialIntelligence");
		System.out.println("The Five Generations of Computers: \n"+ computerGenerations);
	}	
}

3. Output – Example of LinkedHashSet collection in java (example)

The Five Generations of Computers: 
[VacuumTubes, Transistors, IntegratedCircuits, Microprocessors, ArtificialIntelligence]
Scroll to Top