java add to hashset code example

Example 1: HashSet add(E e) method in java

import java.util.HashSet;
public class HashSetAddMethodExample
{
   public static void main(String[] args)
   {
      HashSet<String> hs = new HashSet<String>();
      // populating hash set
      hs.add("violet");
      hs.add("indigo");
      hs.add("blue");
      hs.add("green");
      hs.add("yellow");
      System.out.println("HashSet of colors: " + hs);
   }
}

Example 2: how to push an element in hashset java

HashSet<Integer> set=new HashSet<>();
//adding a new element
set.add(10);

Tags:

Java Example