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

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » Utility Classes » Set 
8.16.3.Sets differ from lists in that they are unordered and cannot contain duplicates of the same element.
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 == nullSystem.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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.