HashSet - add, contains, remove, size methods program






HashSet - add, contains, remove, size methods program.




import java.util.HashSet;
import java.util.Set;
public class HashSetTest {
   public static void main(String args[]) {
          System.out.println("-----1. create Set---");
          Set<String> hashSet = new HashSet<String>();
          System.out.println("\n-----2. adding elements in Set---");
          hashSet.add("ankit");
          hashSet.add("JavaMadeSoEasy");
          System.out.println("hashSet ="+hashSet);
         
          System.out.println("\n------3. contains element  ");
          System.out.println("hashSet.contains = "+hashSet.contains("ankit"));
          System.out.println("\n------4. Remove element at 1st index i.e. mittal");
          hashSet.remove("ankit");  
          System.out.println(hashSet);
          System.out.println("hashSet ="+hashSet);
   }
}
/*OUTPUT
-----1. create Set---
-----2. adding elements in Set---
hashSet =[ankit, JavaMadeSoEasy]
------3. contains element
hashSet.contains = true
------4. Remove element at 1st index i.e. mittal
[JavaMadeSoEasy]
hashSet =[JavaMadeSoEasy]
*/



RELATED LINKS>


HashSet - isEmpty, size and clear methods program


HashSet - iterate using iterator, Enumeration and enhanced for loop program


HashSet - fail-safe or fail-fast iteration using iterator, Enumeration and enhanced for loop program


HashSet - synchronizing using Collections.synchronizedSet program


HashSet - making set unmodifiable using Collections.unmodifiableSet


CopyOnWriteArraySet - add, contains, remove, size methods program


eEdit
Must read for you :