Core Java Tutorial

Core Java Tutorial

Java LinkedHashSet Example Program

LinkedHashSet:

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set.

package com.candidjava.core;
import java.util.LinkedHashSet;
public class LinkedHashsetExample {
 
  
  public static void main(String[] args) {
    LinkedHashSet<String> lhs=new LinkedHashSet<String>();
    lhs.add("hai");
    lhs.add("123");
    lhs.add("mathan");
    lhs.add("llhs");
    lhs.add("mathan");
    lhs.add("llhs");
    lhs.add("ramya");
    lhs.add("suji");
    lhs.add("ravathi");
    lhs.add("sri");
    
    System.out.println("LinkedHashSet ..  "+lhs);
    System.out.println("size ... "+lhs.size());		
    System.out.println("Output will be in insertion  order and dupliate will be removed");
    
  }
  
}
Output:
LinkedHashSet ..  [hai, 123, mathan, llhs, ramya, suji, ravathi, sri]
size ... 8
Output will be in insertion  order and dupliate will be removed