Java Collection Tutorial - Java HashSet.add(E e)








Syntax

HashSet.add(E e) has the following syntax.

public boolean add(E e)

Example

In the following code shows how to use HashSet.add(E e) method.

/*  ww w.  jav  a2 s  .c o  m*/

import java.util.HashSet;

public class Main {
   public static void main(String args[]) {
      HashSet<String>  newset = new HashSet <String> ();
                  
      // populate hash set
      newset.add("Learning"); 
      newset.add("from");
      newset.add("java2s.com");  
            
      // checking elements in hash set
      System.out.println("Hash set values: "+ newset);
   }    
}

The code above generates the following result.