Answers for "remove from hashset in java"

1

HashSet remove(Object o) method in java

import java.util.HashSet;
public class HashSetRemoveObjectMethodExample
{
   public static void main(String[] args)
   {
      HashSet<String> hs = new HashSet<String>();
      hs.add("hello");
      hs.add("world");
      hs.add("java");
      System.out.println("HashSet before using remove(Object o) method: " + hs);
      // remove string "world" from HashSet
      boolean bool = hs.remove("world");
      System.out.println("Has string removed? " + bool);
      System.out.println("HashSet after using remove(Object o) method: " + hs);
   }
}
Posted by: Guest on October-28-2020
-1

HashSet removeAll() method in java

example on removeAll() method for NullPointerException
import java.util.HashSet;
public class HashSetRemoveAllMethodExample
{
   public static void main(String[] args)
   {
      try
      {
         HashSet<Integer> hs1 = new HashSet<Integer>();
         hs1.add(2);
         hs1.add(4);
         hs1.add(6);
         hs1.add(8);
         hs1.add(10);
         // printing hs1
         System.out.println("HashSet before using removeAll() method: " + hs1);
         // create another object of HashSet
         HashSet<Integer> hs2 = null;
         // printing hs2
         System.out.println("Elements to be removed: " + hs2);
         System.out.println("Trying to pass null: ");
         // removing elements from HashSet
         // specified in hs2 using removeAll() method
         hs1.removeAll(hs2);
         System.out.println("HashSet after using removeAll() method: " + hs1);
      }
      catch(NullPointerException ex)
      {
         System.out.println("Exception: " + ex);
      }
   }
}
Posted by: Guest on October-28-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language