Java HashSet add() Method



Description

The add(E e) method is used to add the specified element to this set if it is not already present.If this set already contains the element, the call leaves the set unchanged and returns false.

Declaration

Following is the declaration for java.util.HashSet.add() method.

public boolean add(E e)

Parameters

e − This is the element to be added to this set.

Return Value

The method call returns 'true' if this set did not already contain the specified element.

Exception

NA

Adding an Element to a HashSet of Integers Example

The following example shows the usage of Java HashSet add() method to add entries to the HashSet. We've created a HashSet object of Integer. Then few entries are added using add() method and then set is printed.

package com.tutorialspoint;

import java.util.HashSet;

public class HashSetDemo {
   public static void main(String args[]) {
      
      // create hash set
      HashSet <Integer> newset = new HashSet <>();

      // populate hash set
      newset.add(1); 
      newset.add(2);
      newset.add(3);  

      // checking elements in hash set
      System.out.println("Hash set values: "+ newset);
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Hash set values: [1, 2, 3]

Adding an Element to a HashSet of Strings Example

The following example shows the usage of Java HashSet add() method to add entries to the HashSet. We've created a HashSet object of Strings. Then few entries are added using add() method and then set is printed.

package com.tutorialspoint;

import java.util.HashSet;

public class HashSetDemo {
   public static void main(String args[]) {
      
      // create hash set
      HashSet <String> newset = new HashSet <>();

      // populate hash set
      newset.add("Learning"); 
      newset.add("Easy");
      newset.add("Simply");  

      // checking elements in hash set
      System.out.println("Hash set values: "+ newset);
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Hash set values: [Learning, Simply, Easy]

Adding an Element to a HashSet of Objects Example

The following example shows the usage of Java HashSet add() method to add entries to the HashSet. We've created a HashSet object of Student objects. Then few entries are added using add() method and then set is printed.

package com.tutorialspoint;

import java.util.HashSet;

public class HashSetDemo {
   public static void main(String args[]) {
      
      // create hash set
      HashSet <Student> newset = new HashSet <>();

      // populate hash set
      newset.add(new Student(1, "Julie")); 
      newset.add(new Student(2, "Robert"));
      newset.add(new Student(3, "Adam"));	  

      // checking elements in hash set
      System.out.println("Hash set values: "+ newset);
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result.

Hash set values: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_hashset.htm
Advertisements