LinkedHashSet in Java is a Hashtable and Linked list implementation of the Set interface. It extends the HashSet class and implements the Set interface.
HashSet in java doesn’t maintain the insertion order since elements are stored using hashing mechanism, But if want to store the distinct element in insertion order then we can use LinkedHashSet in Java.
Constructors of LinkedHashSet in Java
- public LinkedHashSet(): Creates a new, empty linked hash set with the default initial capacity (16) and load factor (0.75).
- public LinkedHashSet(Collection<? extends E> c): Creates a new linked hash set with the same elements as the specified collection.
- public LinkedHashSet(int initialCapacity): Creates a new, empty linked hash set with the specified initial capacity and the default load factor (0.75).
- public LinkedHashSet(int initialCapacity, float loadFactor): Creates a new, empty linked hash set with the specified initial capacity and load factor.
import java.util.LinkedHashSet;
public class Test {
public static void main(String[] args) {
// 1. Using public LinkedHashSet():
LinkedHashSet<String> set1 = new LinkedHashSet<>();
set1.add(“ASBG”);
set1.add(“KAIL”);
set1.add(“ASBG”);
System.out.println(“Set1 is: ” + set1);
// Using public LinkedHashSet(Collection<? extends E> c)
LinkedHashSet<String> set2 = new LinkedHashSet<>(set1);
set2.add(“KSOO”);
set2.add(“CHAN”);
System.out.println(“Set2 is: ” + set2);
// Using public LinkedHashSet(int initialCapacity)
LinkedHashSet<String> set3 = new LinkedHashSet<>(20);
set3.add(“DYPT”);
set3.add(“KFLR”);
System.out.println(“Set3 is: ” + set3);
// Using public LinkedHashSet(int initialCapacity, float loadFactor)
LinkedHashSet<String> set4 = new LinkedHashSet<>(20, 0.5F);
set4.add(“NGJD”);
set4.add(“WE45”);
System.out.println(“Set4 is: ” + set4);
}
}
public class Test {
public static void main(String[] args) {
// 1. Using public LinkedHashSet():
LinkedHashSet<String> set1 = new LinkedHashSet<>();
set1.add(“ASBG”);
set1.add(“KAIL”);
set1.add(“ASBG”);
System.out.println(“Set1 is: ” + set1);
// Using public LinkedHashSet(Collection<? extends E> c)
LinkedHashSet<String> set2 = new LinkedHashSet<>(set1);
set2.add(“KSOO”);
set2.add(“CHAN”);
System.out.println(“Set2 is: ” + set2);
// Using public LinkedHashSet(int initialCapacity)
LinkedHashSet<String> set3 = new LinkedHashSet<>(20);
set3.add(“DYPT”);
set3.add(“KFLR”);
System.out.println(“Set3 is: ” + set3);
// Using public LinkedHashSet(int initialCapacity, float loadFactor)
LinkedHashSet<String> set4 = new LinkedHashSet<>(20, 0.5F);
set4.add(“NGJD”);
set4.add(“WE45”);
System.out.println(“Set4 is: ” + set4);
}
}
Output:
Set1 is: [ASBG, KAIL]Set2 is: [ASBG, KAIL, KSOO, CHAN]
Set3 is: [DYPT, KFLR]
Set4 is: [NGJD, WE45]
LinkedHashSet inherits the methods from the Set interface and HashSet class. Following are the methods inherited from the Set interface and HashSet class.
Methods inherited from Set Interface:
- boolean add(E e): Adds the specified element to the set if it is not already present.
- boolean addAll(Collection<? extends E> c): Adds all of the elements in the specified collection to the set if they’re not already present.
- void clear(): Removes all of the elements from the set.
- boolean contains(Object o): Returns true if the set contains the specified element.
- boolean containsAll(Collection<?> c): Returns true if the set contains all of the elements of the specified collection.
- boolean equals(Object o): Compares the specified object with the set for equality.
- int hashCode(): Returns the hash code value for the set.
- boolean isEmpty(): Returns true if the set contains no elements.
- Iterator<E> iterator(): Returns an iterator over the elements in the set.
- boolean remove(Object o): Removes the specified element from the set if it is present (optional operation).
- boolean removeAll(Collection<?> c): Removes from the set all of its elements that are contained in the specified collection (optional operation).
- boolean retainAll(Collection<?> c): Retains only the elements in the set that are contained in the specified collection (optional operation).
- int size(): Returns the number of elements in the set (its cardinality).
- Object[] toArray(): Returns an array containing all of the elements in the set.
- <T> T[] toArray(T[] a): Returns an array containing all of the elements in the set; the runtime type of the returned array is that of the specified array.
Methods inherited from HashSet:
- boolean add(E e): Adds the specified element to this set if it is not already present.
- void clear(): Removes all of the elements from this set.
- Object clone(): Returns an empty HashSet instance the elements are not cloned.
- boolean contains(Object o): Returns true if this set contains the specified element.
- boolean isEmpty(): Returns true if this set contains no elements.
- Iterator<E> iterator(): Returns an iterator over the elements in this set.
- boolean remove(Object o): Removes the specified element from this set if it is present.
- int size(): Returns the number of elements present in the set.
Following is the program for java LinkedHashSet using the above methods.
import java.util.LinkedHashSet;
import java.util.Iterator;
public class Test {
public static void main(String[] args) throws Exception {
LinkedHashSet<String> map1 = new LinkedHashSet<>();
map1.add(“ABCD”);
map1.add(“GSFE”);
map1.add(“HJDK”);
map1.add(“KSHE”);
map1.add(“SBDH”);
System.out.println(“Map1 : ” + map1);
LinkedHashSet<String> map2 = (LinkedHashSet<String>) map1.clone();
map2.addAll(map1);
System.out.println(“Map2 : ” + map2);
map2.clear();
System.out.println(“Map2 after clear: ” + map2);
System.out.println(“Map2 is empty: ” + map2.isEmpty());
map2.add(“GSFE”);
map2.add(“HJAL”);
System.out.println(“Map2 :” + map2);
System.out.println(“Map1 contains GSFE: ” + map1.contains(“GSFE”));
System.out.println(“Map1 contains All from map2: ” + map1.containsAll(map2));
map1.remove(“SBDH”);
System.out.println(“Map1 after remove SBDH: ” + map1);
map1.removeAll(map2);
System.out.println(“Map1 after removeAll map2 elements: ” + map1);
// adding elements in map2
map2.add(“ABCD”);
map2.add(“KSHE”);
System.out.println(“Map2 Updated: ” + map2);
map1.retainAll(map2);
System.out.println(“Map1 after retainAll map2 elements: ” + map1);
System.out.println(“Map1 size: ” + map1.size() + “, Map2 Size: ” + map2.size());
Object[] arr1 = map1.toArray();
String[] arr2 = new String[2];
arr2 = map2.toArray(arr2);
System.out.print(“arr1 elements: “);
for (Object a : arr1) {
System.out.print(” ” + a);
}
System.out.print(“\narr2 elements: “);
for (String a : arr2) {
System.out.print(” ” + a);
}
System.out.print(“\nMap2 elements usign iterator:”);
for (Iterator<String> iterator = map2.iterator(); iterator.hasNext();) {
String string = iterator.next();
System.out.print(” ” + string);
}
}
}
import java.util.Iterator;
public class Test {
public static void main(String[] args) throws Exception {
LinkedHashSet<String> map1 = new LinkedHashSet<>();
map1.add(“ABCD”);
map1.add(“GSFE”);
map1.add(“HJDK”);
map1.add(“KSHE”);
map1.add(“SBDH”);
System.out.println(“Map1 : ” + map1);
LinkedHashSet<String> map2 = (LinkedHashSet<String>) map1.clone();
map2.addAll(map1);
System.out.println(“Map2 : ” + map2);
map2.clear();
System.out.println(“Map2 after clear: ” + map2);
System.out.println(“Map2 is empty: ” + map2.isEmpty());
map2.add(“GSFE”);
map2.add(“HJAL”);
System.out.println(“Map2 :” + map2);
System.out.println(“Map1 contains GSFE: ” + map1.contains(“GSFE”));
System.out.println(“Map1 contains All from map2: ” + map1.containsAll(map2));
map1.remove(“SBDH”);
System.out.println(“Map1 after remove SBDH: ” + map1);
map1.removeAll(map2);
System.out.println(“Map1 after removeAll map2 elements: ” + map1);
// adding elements in map2
map2.add(“ABCD”);
map2.add(“KSHE”);
System.out.println(“Map2 Updated: ” + map2);
map1.retainAll(map2);
System.out.println(“Map1 after retainAll map2 elements: ” + map1);
System.out.println(“Map1 size: ” + map1.size() + “, Map2 Size: ” + map2.size());
Object[] arr1 = map1.toArray();
String[] arr2 = new String[2];
arr2 = map2.toArray(arr2);
System.out.print(“arr1 elements: “);
for (Object a : arr1) {
System.out.print(” ” + a);
}
System.out.print(“\narr2 elements: “);
for (String a : arr2) {
System.out.print(” ” + a);
}
System.out.print(“\nMap2 elements usign iterator:”);
for (Iterator<String> iterator = map2.iterator(); iterator.hasNext();) {
String string = iterator.next();
System.out.print(” ” + string);
}
}
}
Output:
Map1 : [ABCD, GSFE, HJDK, KSHE, SBDH]Map2 : [ABCD, GSFE, HJDK, KSHE, SBDH]
Map2 after clear: []
Map2 is empty: true
Map2 :[GSFE, HJAL]
Map1 contains GSFE: true
Map1 contains All from map2: false
Map1 after remove SBDH: [ABCD, GSFE, HJDK, KSHE]
Map1 after removeAll map2 elements: [ABCD, HJDK, KSHE]
Map2 Updated: [GSFE, HJAL, ABCD, KSHE]
Map1 after retainAll map2 elements: [ABCD, KSHE]
Map1 size: 2, Map2 Size: 4
arr1 elements: ABCD KSHE
arr2 elements: GSFE HJAL ABCD KSHE
Map2 elements usign iterator: GSFE HJAL ABCD KSHE