How to implement set interface in java?

            Set is a collection of elements which does not allow duplicate data. It may be finite or non-finite data. Java collections frameworks include Set interface.

How to implement set interface in java?

            Set interface can be implemented by three ways.

  1. ·       HashSet
  2. ·       LinkedHashSet
  3. ·       TreeSet

1.HashSet:

            This set uses “Hash Table” for storing the data. It follows a constant time performance.

Steps to implement:

  • ·       Include the built in packages java.util.HashSet and java.util.Set.
  • ·       Implement a class with main() function.
  • ·       An object instance is created for HashSet.
  • ·       Insert the data for the set. Here, we add vehicle names to the hash set.
  • ·       Add a duplicate data. But the data is not included in the Set displayed.

Program:

   import java.util.HashSet;

   import java.util.Set;

   public class HashSetEg {

       public static void main(String[] args) {

           Set<String> hashSet1 = new HashSet<>();

           hashSet1.add("Car");

           hashSet1.add("Truck");

           hashSet1.add("Bus");

           hashSet1.add("Van");

           hashSet1.add("Car"); // let us include a duplicate data.

           System.out.println(hashSet1);

       }

   }

Output:

C:\raji\blog>javac HashSetEg.java

C:\raji\blog>java HashSetEg

[Bus, Van, Car, Truck]

Note: you can insert more data also.

2.LinkedHashSet:

            LinkedHashSet follows a linked list for implementation.

Java implementation of LinkedHashSet is given below.

  • ·       Import the built in packages java.util.LinkedHashSet and java.util.Set.
  • ·       A public class with main() function is created.
  • ·       LinkedHashSet object is created.
  • ·       Insert the data using “add” function.
  • ·       Display the data using System.out.println(). If any duplicate is added to the set,it is not accepted.

Program:

   import java.util.LinkedHashSet;

   import java.util.Set;

   public class LinkedHashSetEg {

   public static void main(String[] args) {

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

           linkedHashSet1.add("Coffee");

           linkedHashSet1.add("Tea");

           linkedHashSet1.add("Juice");

           linkedHashSet1.add("Milk");

           linkedHashSet1.add("Coffee"); // Add a duplicate data. it doesnot display in output.

           System.out.println(linkedHashSet1);

       }

   }

Output:

C:\raji\blog>javac LinkedHashSetEg.java

C:\raji\blog>java LinkedHashSetEg

[Coffee, Tea, Juice, Milk]

 3.TreeSet

            TreeSet follows a tree structure to implement this Set Interface. The data’s are displayed in ascending order.

To implement the program as follows.

  • ·       Include the built in packages java.util.Set and java.util.TreeSet.
  • ·       Write java code for creating a class with main().
  • ·       An object is created for TreeSet.
  • ·       Using the object, add the data.
  • ·       Display the data. It automatically display it in ascending order.

Program:

   import java.util.Set;

   import java.util.TreeSet;

   public class TreeSetEg {

       public static void main(String[] args) {

           Set<String> treeSet1 = new TreeSet<>();

           treeSet1.add("Red");

           treeSet1.add("Green");

           treeSet1.add("Yellow");

           treeSet1.add("Orange");

           treeSet1.add("Blue");

           treeSet1.add("Green"); // it is a duplicate data, it is not added in the set

            System.out.println(treeSet1);

       }

   }

Output:

C:\raji\blog>javac TreeSetEg.java

C:\raji\blog>java TreeSetEg

[Blue, Green, Orange, Red, Yellow]

This is the basic implementation of Set interface in java.

No comments:

Post a Comment