Java LinkedHashSet : Collection Class
LinkedHashSet class :
- LinkedHashSet class extends HashSet
- LinkedHashSet uses linked list of its elements in Set
- LinkedHashSet retains the insertion order.
- LinkedHashSet allows to get elements in order of insertion.
- Hash code is used as the index at which the data associated with the key is stored.
- LinkedHashSet does the transformation of the key into its hash code automatically.
Lets create an LinkedHashSet using following line of code -
Example #1 : Create LinkedHashSet
package com.c4learn.collection; import java.util.LinkedHashSet; public class HashSetExample { public static void main(String[] args) { LinkedHashSet<String> hSet = new LinkedHashSet<String>(); hSet.add("A"); hSet.add("B"); hSet.add("K"); hSet.add("F"); hSet.add("C"); hSet.add("H"); System.out.println("Linked hSet : " + hSet); } }
Output :
Linked hSet : [A, B, K, F, C, H]
In the above example, we have created an LinkedHashSet using the following line -
LinkedHashSet<String> hSet = new LinkedHashSet<String>();