Menu
Topics Index
...
`


Collections Framework > Collection Classes >
Siva Nookala - 03 Mar 2017
LinkedHashSet is a class extends HashSet Class In Java. LinkedHashSet has no no other methods other than its super class methods. LinkedHashSet guarantees the order of the elements in which they were added.

The following are the constructors of LinkedHashSet
Constructor Description
LinkedHashSet() Constructs a new, empty linked hash set with the default initial capacity (16) and load factor (0.75).
LinkedHashSet(Collection<? extends E> c) Constructs a new linked hash set with the same elements as the specified collection.
LinkedHashSet(int initialCapacity) Constructs a new, empty linked hash set with the specified initial capacity and the default load factor (0.75).
LinkedHashSet(int initialCapacity, float loadFactor) Constructs a new, empty linked hash set with the specified initial capacity and load factor.
LinkedHashSetDemo
import java.util.*;

class LinkedHashSetDemo
{
    public static void main(String arg[])
    {
        LinkedHashSet<String> lhs = new LinkedHashSet<String>(); // LINE A
        lhs.add("Anderson");
        lhs.add("Bharath");
        lhs.add("Cruse");
        lhs.add("Dinesh");
        System.out.println("Eements retrieved in the order they were inserted : ");
        System.out.println(lhs);
        lhs.remove("Bharath"); // LINE B
        lhs.add("Bhuwaneshwar"); // LINE C
        System.out.println("Set after removing Bharath and adding Bhuwaneshwar : ");
        System.out.println(lhs);
        Object[] names = lhs.toArray();
        System.out.println("Displaying elements in array format : "); // LINE D
        for (Object name : names)
        {
            System.out.print(name + ", ");
        }
        System.out.println();
        lhs.clear(); // LINE E
        System.out.println("Displaying empty set : ");
        System.out.println(lhs);    
    }
}
OUTPUT

Eements retrieved in the order they were inserted :
[Anderson, Bharath, Cruse, Dinesh]
Set after removing Bharath and adding Bhuwaneshwar :
[Anderson, Cruse, Dinesh, Bhuwaneshwar]
Displaying elements in array format :
Anderson, Cruse, Dinesh, Bhuwaneshwar,
Displaying empty set :
[]

DESCRIPTION

In the above program we demonstrated how LinkedHashSet works. At first at LINE A we have created a LinkedHashSet and added some names to it and displayed it and at LINE B we have removed Bharath from the set using remove method and added Bhuwaneshwar to the at set at LINE C using add method and displayed. At LINE D we have created an array of Objects using toArray method and displayed each element in the array. At LINE E we have cleared all the elements in the set using clear method and displayed the empty set.

THINGS TO TRY
  • Now create a LinkedHasSet of Integers and add numbers form 1 to 5
  • Create a clone for the set in the program using clone method. Use the sample code below.
    lhs.clone();
  • Check whether Bharath exits in the set using contains method. Use the sample code below.
    lhs.contains("Bharath");

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.