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

Popular posts from this blog

Big Data Analytics Programs

  List of Programs for Big Data Analytics   CLICK ON ME 1.  Implement the following Data structures in Java       a)  Linked Lists            b)   Stacks       c)  Queues     d)   Set            e)   Map 2.  Perform setting up and Installing Hadoop in its three operating modes:      Standalone,     Pseudo distributed,     Fully distributed. 3.  Implement the following file management tasks in Hadoop:    a) Adding files and directories    b) Retrieving files    c) Deleting files 4. Run a basic Word Count Map Reduce program to understand Map Reduce Paradigm. 5. Write a Map Reduce program that mines weather data.     Weather sensors collecting data every hour at many locations across the globe gather a large volume of log data, which is a good candidate for analysis with MapReduce since it is semi-structured and record-oriented. 6. Implement Matrix Multiplication with Hadoop Map Reduce 7. Write a MapReduce program to count the occurrence of similar words in a file. Use partitioner to part

How to Install Parrot Operating System in Virtual Box using OVA

Step by Step Process of Parrot OS Installation What is Parrot OS Parrot is a free and open-source Linux system based on Debian that is popular among security researchers, security experts, developers, and privacy-conscious users. It comes with cyber security and digital forensics arsenal that is totally portable. It also includes everything you'll need to make your own apps and protect your online privacy. Parrot is offered in Home and Security Editions, as well as a virtual machine and a Docker image, featuring the KDE and Mate desktop environments. Features of Parrot OS The following are some of the features of Parrot OS that set it apart from other Debian distributions: Tor, Tor chat, I2P, Anonsurf, and Zulu Crypt, which are popular among developers, security researchers, and privacy-conscious individuals, are included as pre-installed development, forensics, and anonymity applications. It has a separate "Forensics Mode" that does not mount any of the system's hard

LAB

 Big Data Analytics Lab Programs 1.        Implement the following Data structures in Java for Linked Lists 2.    Perform setting up and Installing Hadoop in its three operating modes: Standalone, Pseudo distributed, Fully distributed 3.        Implement the following Data structures in Java for Stack 4.        Install and Run Pig then write Pig Latin scripts to sort, group, join, project, and filter your data 5.        Implement the following Data structures in Java for Queues 6.        Write a MapReduce program to search for a specific keyword in a file 7.        Implement the following Data structures in Java for Set 8.      Write a MapReduce program to count the occurrence of similar words in a file. Use partitioner to partition key based on alphabets 9.        Implement the following Data structures in Java for Map 10.   Implement the following file management tasks in Hadoop: 1. Adding files and directories 2. Retrieving files 3. Deleting files 11.    Run a basic Word Count Map R