Set

来源:互联网 发布:充话费软件利润 编辑:程序博客网 时间:2024/05/22 10:44
  1. /**
  2.  * @(#)SetExample.java
  3.  *
  4.  *
  5.  * @author 
  6.  * @version 1.00 2008/9/28
  7.  */
  8. import java.util.*;
  9. public class SetExample {
  10.     
  11.     public SetExample() {
  12.         
  13.     }
  14.     public static void main(String []args)
  15.     {
  16.         Set<Integer> s1=new HashSet<Integer>();
  17.         Collection<Integer> s2=new HashSet<Integer>();
  18.         int i;
  19.         for( i=0; i<10; i++)
  20.             s1.add(i);
  21.         for( i=10; i<100; i++)
  22.             s2.add(i);
  23.             
  24.         for( i=10; i<20; i++)
  25.             s2.add(i);
  26.         System.out.println("before retainAll s1:"+s1);
  27.         System.out.println("before retainAll s2:"+s2);
  28.         s2.retainAll(s1);
  29.         System.out.println("after retainAll s2:"+s2);
  30.         s2.addAll(s1);
  31.         System.out.println("afetr addAll s2"+s2);
  32.         s2.clear();
  33.         System.out.println("after clear:s2"+s2);
  34.     
  35.                 
  36.         
  37.     }
  38.     
  39.     
  40. }
  41. //result:
  42. 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[]

 

原创粉丝点击