HashSet - isEmpty, size and clear methods program







HashSet - isEmpty, size and clear methods program.




import java.util.HashSet;
import java.util.Set;
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
*/
public class HashSetTest {
   public static void main(String args[]) {
          // creates array with initial capacity of 10.
          Set<String> hashSet = new HashSet<String>();
          System.out.println("--------add element ");
          hashSet.add("ankit");
          hashSet.add("mittal");
          hashSet.add("javaMadeSoEasy");
          System.out.println("hashSet = "+hashSet);
          System.out.println("hashSet.isEmpty() = "+hashSet.isEmpty());
          System.out.println("\n--------clear list - remove all elements from set");
          hashSet.clear();
         
          System.out.println("hashSet.isEmpty() = "+hashSet.isEmpty());
          System.out.println("hashSet = "+hashSet);
         
   }
}
/*OUTPUT
--------add element
hashSet = [ankit, mittal, javaMadeSoEasy]
hashSet.isEmpty() = false
--------clear list - remove all elements from set
hashSet.isEmpty() = true
hashSet = []
*/



RELATED LINKS>


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


Must read for you :

No comments:

Post a Comment