Skip to main content

Implement Set using java

  •  Implement Set using java

 HashSet

   It is used to create collection which uses hash table for storage of data and implements Set Interface.

 

Features

·                          It stores the elements by using mechanism called Hashing

·                          It contains unique elements only.

·                          It does not maintain insertion order  

Constructors

·                      HashSet()-initializes empty HashSet

·                      HashSet(Collection c)-constructs HashSet by using the elements of Collection

 Methods

·                   add(Object o)-adds the specified element to this set if it is not already present

·                   remove(Object o)-removes specified element from set

·                   clear()-removes all elements from the set

·                   size()-counts the number of elements in set

·                   iterator()- iterates over the elements in set


Program on HashSet

import java.util.Set;
import java.util.HashSet;
import java.util.Scanner;//Scanner class reads data from input stream
class Hashsetdemo
{
 public static void main(String[] args)
 {
  Set<Integer> obj=new HashSet<Integer>();//Set is interface and HashSet is class
  Scanner s=new Scanner(System.in);
  while(true)
  {
   System.out.println("1.Add");
   System.out.println("2.Remove");
   System.out.println("3.Display");
   System.out.println("4.Quit");
   System.out.println("Enter choice");
   int choice=s.nextInt();
   switch(choice)
   {
     case 1:System.out.println("Enter the element to be added to hashset");
            int e=s.nextInt();
            obj.add(e);//add element to hash set
            break;
     case 2:if(obj.isEmpty())//checks whether hashset is empty or not
            System.out.println("Hashset is empty");
            else
            {
            System.out.println("Enter the element to be removed from hashset");
            e=s.nextInt();
            
            
            if(obj.contains(e))//contains() checks whether hashset contains element or not
            { 
            obj.remove(e);
            System.out.println(e+" is removed from HashSet successfully");
            }
            else
            System.out.println("Element not found in hashset to be deleted");
            }
            break;
     case 3:if(obj.isEmpty())
             System.out.println("Hashset is empty");
            else
            {
            System.out.println("HashSet elements");
            for(Integer i:obj)//enhanced for loop
            System.out.println(i);
           //prints the elements using size()
          // convert hashset  to array
           // Integer[] i=obj.toArray(new Integer[obj.size()]);
            //for(int x=0;x<i.length;++x)
            //System.out.println(i[x]);
            // using Iterator interface to print all the elements in hashset
            //java.util.Iterator<Integer> y=obj.iterator();
            //while(y.hasNext())// checks whether hashset contains any more elements or not
            //System.out.println(y.next());//prints element
            }
            break;
     case 4:System.exit(0);
     default:System.out.println("Wrong choice");
    }
   }
  }
}
Expected Output
1.Add
2.Remove
3.Display
4.Quit
Enter choice
1
Enter the element to be added to hashset
10
1.Add
2.Remove
3.Display
4.Quit
Enter choice
1
Enter the element to be added to hashset
20
1.Add
2.Remove
3.Display
4.Quit
Enter choice
1
Enter the element to be added to hashset
30
1.Add
2.Remove
3.Display
4.Quit
Enter choice
1
Enter the element to be added to hashset
40
1.Add
2.Remove
3.Display
4.Quit
Enter choice
3
HashSet elements
20
40
10
30
1.Add
2.Remove
3.Display
4.Quit
Enter choice
2
Enter the element to be removed from hashset
12
Element not found in hashset to be deleted
1.Add
2.Remove
3.Display
4.Quit
Enter choice
2
Enter the element to be removed from hashset
40
40 is removed from HashSet successfully
1.Add
2.Remove
3.Display
4.Quit
Enter choice
3
HashSet elements
20
10
30
1.Add
2.Remove
3.Display
4.Quit
Enter choice
2
Enter the element to be removed from hashset
30
30 is removed from HashSet successfully
1.Add
2.Remove
3.Display
4.Quit
Enter choice
3
HashSet elements
20
10
1.Add
2.Remove
3.Display
4.Quit
Enter choice
2
Enter the element to be removed from hashset
10
10 is removed from HashSet successfully
1.Add
2.Remove
3.Display
4.Quit
Enter choice
3
HashSet elements
20
1.Add
2.Remove
3.Display
4.Quit
Enter choice
2
Enter the element to be removed from hashset
20
20 is removed from HashSet successfully
1.Add
2.Remove
3.Display
4.Quit
Enter choice
2
Hashset is empty
1.Add
2.Remove
3.Display
4.Quit
Enter choice
3
Hashset is empty
1.Add
2.Remove
3.Display
4.Quit
Enter choice
4
------------------------------------------------------------------------------------------------------------------
LinkedHashSet

This class is used to create collection which extends HashSet class and implements Set interface.

Features
  • It stores the elements by using mechanism called Hashing
  • It contains unique elements only.
  • It maintains insertion order
LinkedHashSet program

import java.util.Set; import java.util.LinkedHashSet; import java.util.Scanner;//Scanner class reads data from input stream class Linkedhashsetdemo { public static void main(String[] args) { Set<Integer> obj=new LinkedHashSet<Integer>();//Set is interface and LinkedHashSet is class Scanner s=new Scanner(System.in); while(true) { System.out.println("1.Add"); System.out.println("2.Remove"); System.out.println("3.Display"); System.out.println("4.Quit"); System.out.println("Enter choice"); int choice=s.nextInt(); switch(choice) { case 1:System.out.println("Enter the element to be added to linked hashset"); int e=s.nextInt(); obj.add(e);//add element to linked hash set break; case 2:if(obj.isEmpty())//checks whether linked hashset is empty or not System.out.println("Linked Hashset is empty"); else { System.out.println("Enter the element to be removed from Linked hashset"); e=s.nextInt(); if(obj.contains(e))//contains() checks whether linkedhashset contains element or not { obj.remove(e); System.out.println(e+" is removed from LinkedHashSet successfully"); } else System.out.println("Element not found in linkedehashset to be deleted"); } break; case 3:if(obj.isEmpty()) System.out.println("LinkedHashset is empty"); else { System.out.println("LinkedHashSet elements"); for(Integer i:obj)//enhanced for loop System.out.println(i); //prints the elements using size() // convert hashset to array // Integer[] i=obj.toArray(new Integer[obj.size()]); //for(int x=0;x<i.length;++x) //System.out.println(i[x]); // using Iterator interface to print all the elements in hashset //java.util.Iterator<Integer> y=obj.iterator(); //while(y.hasNext())// checks whether hashset contains any more elements or not //System.out.println(y.next());//prints element } break; case 4:System.exit(0); default:System.out.println("Wrong choice"); } } } } Expected Output 1.Add 2.Remove 3.Display 4.Quit Enter choice 1 Enter the element to be added to linked hashset 10 1.Add 2.Remove 3.Display 4.Quit Enter choice 1 Enter the element to be added to linked hashset 20 1.Add 2.Remove 3.Display 4.Quit Enter choice 1 Enter the element to be added to linked hashset 30 1.Add 2.Remove 3.Display 4.Quit Enter choice 1 Enter the element to be added to linked hashset 40 1.Add 2.Remove 3.Display 4.Quit Enter choice 1 Enter the element to be added to linked hashset 50 1.Add 2.Remove 3.Display 4.Quit Enter choice 3 LinkedHashSet elements 10 20 30 40 50 1.Add 2.Remove 3.Display 4.Quit Enter choice 2 Enter the element to be removed from Linked hashset 30 30 is removed from LinkedHashSet successfully 1.Add 2.Remove 3.Display 4.Quit Enter choice 2 Enter the element to be removed from Linked hashset 25 Element not found in linkedehashset to be deleted 1.Add 2.Remove 3.Display 4.Quit Enter choice 3 LinkedHashSet elements 10 20 40 50 1.Add 2.Remove 3.Display 4.Quit Enter choice 2 Enter the element to be removed from Linked hashset 40 40 is removed from LinkedHashSet successfully 1.Add 2.Remove 3.Display 4.Quit Enter choice 2 Enter the element to be removed from Linked hashset 10 10 is removed from LinkedHashSet successfully 1.Add 2.Remove 3.Display 4.Quit Enter choice 3 LinkedHashSet elements 20 50 1.Add 2.Remove 3.Display 4.Quit Enter choice 2 Enter the element to be removed from Linked hashset 20 20 is removed from LinkedHashSet successfully 1.Add 2.Remove 3.Display 4.Quit Enter choice 2 Enter the element to be removed from Linked hashset 50 50 is removed from LinkedHashSet successfully 1.Add 2.Remove 3.Display 4.Quit Enter choice 2 Linked Hashset is empty 1.Add 2.Remove 3.Display 4.Quit Enter choice 3 LinkedHashset is empty 1.Add 2.Remove 3.Display 4.Quit Enter choice 4

TreeSet
It uses the tree for storage of elements which are maintained in increasing order.
Features
•	Contains unique elements
•	Fast retrieval and access of elements
•	Maintains ascending order
            Constructors
•	TreeSet()- constructs an empty TreeSet that will be sorted in an ascending order
•	TreeSet(Collection c)-constructs new TreeSet that contains the elements of collection
             Methods
•	add(Object o)-adds the specified element to this set if it is not already present
•	remove(Object o)-removes specified element from set
•	clear()-removes all elements from the set
•	size()-counts the number of elements in set
•	first()-returns the lowest element currently in this sorted set
•	last()-returns highest element currently in this sorted set
•	iterator()- iterates over the elements in set
TreeSet program
import java.util.Set;
import java.util.TreeSet;
import java.util.Scanner;//Scanner class reads data from input stream
class Treesetdemo
{
 public static void main(String[] args)
 {
  Set<Integer> obj=new TreeSet<Integer>();//Set is interface and TreeSet is class
  Scanner s=new Scanner(System.in);
  while(true)
  {
   System.out.println("1.Add");
   System.out.println("2.Remove");
   System.out.println("3.Display");
   System.out.println("4.Quit");
   System.out.println("Enter choice");
   int choice=s.nextInt();
   switch(choice)
   {
     case 1:System.out.println("Enter the element to be added to treeset");
            int e=s.nextInt();
            obj.add(e);//add element to tree set
            break;
     case 2:if(obj.isEmpty())//checks whether treeset is empty or not
            System.out.println("Hashset is empty");
            else
            {
            System.out.println("Enter the element to be removed from treeset");
            e=s.nextInt();
            if(obj.contains(e))//contains() checks whether treeset contains element or not
            { 
            obj.remove(e);
            System.out.println(e+" is removed from treeSet successfully");
            }
            else
            System.out.println("Element not found in treeset to be deleted");
            }
            break;
     case 3:if(obj.isEmpty())
             System.out.println("treeset is empty");
            else
            {
            System.out.println("TreeSet elements");
            for(Integer i:obj)//enhanced for loop
            System.out.println(i);
           //prints the elements using size()
          // convert hashset  to array
           // Integer[] i=obj.toArray(new Integer[obj.size()]);
            //for(int x=0;x<i.length;++x)
            //System.out.println(i[x]);
            // using Iterator interface to print all the elements in hashset
            //java.util.Iterator<Integer> y=obj.iterator();
            //while(y.hasNext())// checks whether hashset contains any more elements or not
            //System.out.println(y.next());//prints element
            }
            break;
     case 4:System.exit(0);
     default:System.out.println("Wrong choice");
    }
   }
  }
}

Expected Output
1.Add
2.Remove
3.Display
4.Quit
Enter choice
1
Enter the element to be added to treeset
20
1.Add
2.Remove
3.Display
4.Quit
Enter choice
1
Enter the element to be added to treeset
10
1.Add
2.Remove
3.Display
4.Quit
Enter choice
1
Enter the element to be added to treeset
25
1.Add
2.Remove
3.Display
4.Quit
Enter choice
1
Enter the element to be added to treeset
45
1.Add
2.Remove
3.Display
4.Quit
Enter choice
1
Enter the element to be added to treeset
42
1.Add
2.Remove
3.Display
4.Quit
Enter choice
3
TreeSet elements
10
20
25
42
45
1.Add
2.Remove
3.Display
4.Quit
Enter choice
2
Enter the element to be removed from treeset
25
25 is removed from treeSet successfully
1.Add
2.Remove
3.Display
4.Quit
Enter choice
3
TreeSet elements
10
20
42
45
1.Add
2.Remove
3.Display
4.Quit
Enter choice
2
Enter the element to be removed from treeset
44
Element not found in treeset to be deleted
1.Add
2.Remove
3.Display
4.Quit
Enter choice
2
Enter the element to be removed from treeset
10
10 is removed from treeSet successfully
1.Add
2.Remove
3.Display
4.Quit
Enter choice
3
TreeSet elements
20
42
45
1.Add
2.Remove
3.Display
4.Quit
Enter choice
4


Comments