Sets differ from lists in that they are unordered and cannot contain duplicates of the same element. : Set « Utility Classes « SCJP






import java.util.HashSet;
import java.util.Iterator;
   
public class MainClass {
 public static void main(String args[]){
    HashSet set = new HashSet();
    set.add("This");
    set.add("is");
    set.add("is");
    set.add("a");
    set.add("a");
    set.add(null);
    set.add("test");
    displaySet(set);
 }
 static void displaySet(HashSet set) {
  System.out.println("The size of the set is: "+set.size());
  Iterator i = set.iterator();
  while(i.hasNext()){
   Object o = i.next();
   if(o == null) System.out.println("null");
   else System.out.println(o.toString());
  }
 }
}








8.16.Set
8.16.1.Sets may not contain duplicate elements.
8.16.2.An Iterator from HashSet presents elements in an unpredictable random order.
8.16.3.Sets differ from lists in that they are unordered and cannot contain duplicates of the same element.
8.16.4.Using Sets