Java tutorial
import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; class SetDemo { public static void main(String[] args) { String[] letters = { "A", "B", "C", "D", "E" }; Set s = new HashSet(); for (int i = 0; i < letters.length; i++) s.add(letters[i]); if (!s.add(letters[0])) System.out.println("Cannot add a duplicate.\n"); Iterator iter = s.iterator(); while (iter.hasNext()) System.out.println(iter.next()); System.out.println(""); SortedSet ss = new TreeSet(); for (int i = 0; i < letters.length; i++) ss.add(letters[i]); if (!ss.add(letters[0])) System.out.println("Cannot add a duplicate.\n"); iter = ss.iterator(); while (iter.hasNext()) System.out.println(iter.next()); } }