Jacob69
0
Q:

hashset 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);
   }
}
3
// 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);
      }
   }
}
2
// 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);
   }
}
1
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);
   }
}
1
HashSet<String> hset = new HashSet<String>();
// Adding elements to the HashSet
hset.add("Apple");
hset.add("Mango");
2
// Java program to demonstrate working of HashSet 
import java.util.*; 
  
class HashSetDemo { 
  
    // Main Method 
    public static void main(String[] args) 
    { 
        HashSet<String> h = new HashSet<String>(); 
  
        // Adding elements into HashSet usind add() 
        h.add("India"); 
        h.add("Australia"); 
        h.add("South Africa"); 
        h.add("India"); // adding duplicate elements 
  
        // Displaying the HashSet 
        System.out.println(h); 
        System.out.println("List contains India or not:"
                           + h.contains("India")); 
  
        // Removing items from HashSet using remove() 
        h.remove("Australia"); 
        System.out.println("List after removing Australia:"
                           + h); 
  
        // Iterating over hash set items 
        System.out.println("Iterating over list:"); 
        Iterator<String> i = h.iterator(); 
        while (i.hasNext()) 
            System.out.println(i.next()); 
    } 
}
0

New to Communities?

Join the community