Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – HashSet

Declaration

class HashSet<E>

HashSet extends abstract class AbstractSet & implements the Set interface.

It stores elements using hashing mechanism.

Duplicate are not allowed.

Elements insertion order is not maintained.

List of HashSet Constructors (CLICK HERE)

List of HashSet Methods (CLICK HERE)

Non Generic Demo

In non-generic demo, we have used non-generic HashSet class, constructor & methods. Therefore it can contain any type of elements (heterogeneous elements) as shown in program below:

Example

 import java.util.HashSet;

 public class HashSetNonGenDemo 
 {
     public static void main(String args[])
     {
         HashSet hs = new HashSet();
         hs.add("Java");
         hs.add(10);
         hs.add('X');
         hs.add(44.33);
         System.out.println(hs);
     }
 }

Output

[Java, 44.33, X, 10]

If we want HashSet must contain elements of same type then we must use generic HashSet.

Generic Demo

In the program below we have used generic HashSet that can contain only String type of elements.
If we tried to insert element other than String, we will get compile time error.

Example – I

 import java.util.HashSet;

 public class HashSetGenDemo1 
 {
     public static void main(String args[])
     {
         HashSet<String> hs = new HashSet<String>();
         hs.add("I");
         hs.add("Love");
         hs.add("Java");
     //  hs.add(30); // The method add(String)
                     // in the type HashSet<String> 
		        // is not applicable for the 
		        // arguments (int)
        
         System.out.println(hs);
     }
 }

Output

[Java, Love, I]

Example – II

 import java.util.HashSet;

 public class HashSetGenDemo2 
 {
     public static void main(String args[])
     {
         HashSet<Integer> hs = new HashSet<Integer>();
         hs.add(10);
         hs.add(20);
         hs.add(30);
         hs.add(40);
         System.out.println(hs);
         System.out.println("Does HashSet contains 50? :: "+hs.contains(50));
         System.out.println("Does HashSet contains 20? :: "+hs.contains(20));
         hs.remove(30);
         System.out.println("\nAfter removing 30");
         System.out.println(hs);
     }
 }

Output

[20, 40, 10, 30]
Does HashSet contains 50? :: false
Does HashSet contains 20? :: true

After removing 30
[20, 40, 10]

Example – III

 import java.util.HashSet;

 class Employee 
 {
     int emp_id;
     String name;
     double salary;
	
     Employee(int i, String s, double d)
     {
         emp_id = i;
         name = s;
         salary = d;
     }
 }

 class HashSetGenDemo3 
 {
     public static void main(String args[])
     {
         Employee e1 = new Employee(22,"DAVID", 65000.98);
         Employee e2 = new Employee(55,"JAMES", 78000.69);
         Employee e3 = new Employee(11,"RICKY", 45987.45);
         Employee e4 = new Employee(99,"JACK", 130006.68);
		
         HashSet<Employee> hs = new HashSet<Employee>();
		
         hs.add(e1);
         hs.add(e2);
         hs.add(e3);
         hs.add(e4);
		
         for(Employee e:hs)
         {
             System.out.println(e.emp_id+"  "+e.name+"  "+e.salary);
         }
    }
 }

Output

11 RICKY 45987.45
22 DAVID 65000.98
55 JAMES 78000.69
99 JACK 130006.68

 

Leave a reply