Mega Code Archive

 
Categories / Java / Collections Data Structure
 

Putting your own type in a Set

// : c11:Set2.java // Putting your own type in a Set. // From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002 // www.BruceEckel.com. See copyright notice in CopyRight.txt. import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Set; import java.util.TreeSet; public class Set2 {   public static Set fill(Set a, int size) {     for (int i = 0; i < size; i++)       a.add(new Integer(i));     return a;   }   public static void test(Set a) {     fill(a, 10);     fill(a, 10); // Try to add duplicates     fill(a, 10);     a.addAll(fill(new TreeSet(), 10));     System.out.println(a);   }   public static void main(String[] args) {     test(new HashSet());     test(new TreeSet());     test(new LinkedHashSet());   } } ///:~