HashSet in Java

import java.util.HashSet;
 
public class myHashsetExample {
   public static void main(String[] args) {
      HashSet<String> languages = new HashSet<String>();
      languages.add("hindi");
      languages.add("english");
      languages.add("spanish");
      System.out.println("The number of languages is " + languages.size());
      for (String name : languages) {
         System.out.println(name);
      }
      languages.remove("hindi");
      languages.add("tamil");
      String name = "hindi";
      if (languages.contains(name)) {
         System.out.println(name + " is in the collection.");
      } else {
         System.out.println(name + " is not in the collection.");
      }
      for (String n : languages) {
         System.out.println(n);
      }
      languages.clear();
      System.out.println("The number of languages is " + languages.size());
   }
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.