java hash set code example

Example 1: hashset in java

Let’s see java hashset example.

import java.util.HashSet;
public class HashSetExample
{
   public static void main(String[] args)
   {
      HashSet<String> hs = new HashSet<>();
      hs.add("Banana");
      hs.add("Orange");
      hs.add("Apple");
      hs.add("Pineapple");
      hs.add("Mango");
      System.out.println(hs);
   }
}

Example 2: java hashset

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

Example 3: HashSet h = new HashSet();

HashSet<String> h = new HashSet<String>();

Example 4: hashset in java

- HashSet can have null, order is not guaranteed

Tags:

Java Example