Java LinkedHashSet : Collection Class

LinkedHashSet class :

  1. LinkedHashSet class extends HashSet
  2. LinkedHashSet uses linked list of its elements in Set
  3. LinkedHashSet retains the insertion order.
  4. LinkedHashSet allows to get elements in order of insertion.
  5. Hash code is used as the index at which the data associated with the key is stored.
  6. 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>();


    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>();