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.

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.