- /**
- * @(#)SetExample.java
- *
- *
- * @author
- * @version 1.00 2008/9/28
- */
- import java.util.*;
- public class SetExample {
- public SetExample() {
- }
- public static void main(String []args)
- {
- Set<Integer> s1=new HashSet<Integer>();
- Collection<Integer> s2=new HashSet<Integer>();
- int i;
- for( i=0; i<10; i++)
- s1.add(i);
- for( i=10; i<100; i++)
- s2.add(i);
- for( i=10; i<20; i++)
- s2.add(i);
- System.out.println("before retainAll s1:"+s1);
- System.out.println("before retainAll s2:"+s2);
- s2.retainAll(s1);
- System.out.println("after retainAll s2:"+s2);
- s2.addAll(s1);
- System.out.println("afetr addAll s2"+s2);
- s2.clear();
- System.out.println("after clear:s2"+s2);
- }
- }
- //result:
- before retainAll s1:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
before retainAll s2:[10, 11, 12, 13, 14, 15, 17, 16, 19, 18, 21, 20, 23, 22, 25, 24, 27, 26, 29, 28, 31, 30, 34, 35, 32, 33, 38, 39, 36, 37, 42, 43, 40, 41, 46, 47, 44, 45, 51, 50, 49, 48, 55, 54, 53, 52, 59, 58, 57, 56, 63, 62, 61, 60, 68, 69, 70, 71, 64, 65, 66, 67, 76, 77, 78, 79, 72, 73, 74, 75, 85, 84, 87, 86, 81, 80, 83, 82, 93, 92, 95, 94, 89, 88, 91, 90, 98, 99, 96, 97]
after retainAll s2:[]
afetr addAll s2[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
after clear:s2[]