What is LinkedHashSet collection in java (class hierarchy & example)
Yogesh
Hashtable and linked list implementation of the Set interface, with predictable iteration order.
LinkedHashSet maintains the Insertion order of elements using LinkedList
LinkedHashSet is UnSynchronized and not thread safe.
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.
LinkedHashSet offers constant time performance for the basic operations like add, remove, contains and size.
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]