hashset in java code example

Example 1: how to push an element in hashset java

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

Example 2: HashSet h = new HashSet();

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

Example 3: hashset in java

// java hashmap example
import java.util.HashMap;
public class HashMapExample 
{
   public static void main(String[] args) 
   {
      HashMap<Integer, String> hm = new HashMap<Integer, String>();
      // add elements
      hm.put(10,"Apple");
      hm.put(20,"Banana");
      hm.put(30,"Cherry");
      hm.put(40,"Dragonfruit");
      // print HashMap elements
      System.out.println("HashMap elements: " + hm);
   }
}

Tags:

Java Example