hashset java 8 code example

Example 1: hashset in java

// iterate hashset in java
import java.util.HashSet;
public class HashSetForEach 
{
   public static void main(String[] args) 
   {
      HashSet<String> hs = new HashSet<String>();
      hs.add("Hello"); 
      hs.add("world"); 
      hs.add("Java"); 
      for(String str : hs)
      {
         System.out.println(str);
      }
   }
}

Example 2: java hashset

HashSet<String> hset = new HashSet<String>();
// Adding elements to the HashSet
hset.add("Apple");
hset.add("Mango");