hashset.add 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);

Example 3: java hashset

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

Tags:

Java Example