LinkedHashSet implementation in java

              HashSet+LinkedList = LinkedHashSet. A special feature of this structure is maintaining the data like a HashSet and adding a data like a linked list.

Java has the built-in package for this data structure. Let us implement it as follows.

Steps:

  • Import the built in header files LinkedHashSet and Set.
  • Include a main function with a public class. An object is created for LinkedHashSet as lHashSet1.
  • Using add() function, data elements are added.
  • Function contains() checks the particular data element is available or not and returns value according that.
  • Remove() deletes a particular element.
  • For loop is used to iterate the LinkedHashset.
  • Size() gives you the number of data elements in the LinkedHashSet.
  • To clear the data in the LinkedHashSet, use the clear() function.
  • isEmpty() checks your LinkedHashSet is empty or not.

Program:

import java.util.LinkedHashSet;

import java.util.Set;

public class LinkedHashSetEg1 {

    public static void main(String[] args) {

        // Create a HashSet as linked list

        Set<String> lHashSet1 = new LinkedHashSet<>();

        // insert the elements to the LinkedHashSet

        lHashSet1.add("Rose");

        lHashSet1.add("Lotus");

        lHashSet1.add("Jasmine");

        lHashSet1.add("Sun flower");

        lHashSet1.add("Marigold");

        // Display the hashset

        System.out.println("The elements in the LinkedHashSet: " + lHashSet1);

        // Search an element

        boolean CFlag = lHashSet1.contains("Jasmine");

        if (CFlag)

        System.out.println("The jasmine flower is available in the Hash Set");

        // Delete an element from the Linked HashSet

        lHashSet1.remove("Marigold");

        System.out.println("LinkedHashSet after removing Marigold: " + lHashSet1);

        // Iteration

        System.out.println("Iteration:");

        for (String flower : lHashSet1) {

            System.out.println(flower);

        }

        // Print the size of the LinkedHashSet

        int sizelh = lHashSet1.size();

        System.out.println("The size of the LinkedHashSet is:" + sizelh);

        // Clear the Hash Set

        lHashSet1.clear();

        if (lHashSet1.isEmpty())

        System.out.println("The Linked HashSet is Empty now");

    }

}

Output:

C:\raji\blog>javac LinkedHashSetEg1.java

 

C:\raji\blog>java LinkedHashSetEg1

The elements in the LinkedHashSet: [Rose, Lotus, Jasmine, Sun flower, Marigold]

The jasmine flower is available in the Hash Set

LinkedHashSet after removing Marigold: [Rose, Lotus, Jasmine, Sun flower]

Iteration:

Rose

Lotus

Jasmine

Sun flower

The size of the LinkedHashSet is:4

The Linked HashSet is Empty nows

That’s all. The implementation of LinkedHashSet in java is implemented successfully. It is useful when you want unique data elements.

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.