Collection中的基本方法

来源:互联网 发布:百成玻璃优化排版软件 编辑:程序博客网 时间:2024/06/06 03:19

 Collection接口

Collection是最基本的集合接口!

该接口定义了集合框架中最共性的功能!

最终使用的时候,其实使用的是该框架最子类的对象!

下面挨个探讨一下Collection接口中的基本方法:

1、添加方法:

     方法a:  boolean add(object e);  一次添加一个元素

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import java.util.ArrayList;  
  2. import java.util.Collection;  
  3.   
  4. public class Main_one {  
  5.     public static void main(String[] args)  
  6.     {  
  7.         Collection coll=new ArrayList();  
  8.         collectionDemo(coll);  
  9.     }  
  10.     public static void collectionDemo(Collection coll)  
  11.     {  
  12.         // 导包快捷键:ctrl+shift+o  
  13.         // 1、添加元素  
  14.         coll.add("abc1");  
  15.         coll.add("abc2");  
  16.         coll.add("abc3");  
  17.         System.out.println(coll);  
  18.     }  
  19. }  
   方法b: boolean addAll(Collection c);   将一个参数容器中的元素添加到当前容器中

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import java.util.ArrayList;  
  2. import java.util.Collection;  
  3.   
  4. public class Main_one {  
  5.     public static void main(String[] args)  
  6.     {  
  7.         // Collection中带All的方法  
  8.         Collection c1=new ArrayList();  
  9.         Collection c2=new ArrayList();  
  10.         collectionAllDemo(c1,c2);  
  11.     }  
  12.     // Collection中带All的方法的实现  
  13.     public static void collectionAllDemo(Collection c1,Collection c2)  
  14.     {  
  15.         // 给这两个集合中存储一些元素  
  16.         c1.add("abcd1");  
  17.         c1.add("abcd2");  
  18.         c1.add("abcd3");  
  19.         c1.add("abcd4");  
  20.         c1.add("abcd5");  
  21.           
  22.         c2.add("abcd3");  
  23.         c2.add("abcd4");  
  24.         c2.add("abcd6");  
  25.           
  26.         System.out.println(c1);  
  27.         // 输出的是[abcd1, abcd2, abcd3, abcd4, abcd5]  
  28.           
  29.         // 把c2中的元素添加到c1中  
  30.         c1.addAll(c2);  
  31.         System.out.println(c1);  
  32.         // 输出的是[abcd1, abcd2, abcd3, abcd4, abcd5, abcd3, abcd4, abcd6]  
  33.     }  
  34. }  


2、删除方法:

      方法a:  boolean remove(object e);   删除一个指定对象

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.               // 2、删除元素  
  2. coll.remove("abc2");  
  3. System.out.println(coll);  
  4. // 输出的是[abc1, abc3]  
     方法b: boolean removeAll(Collection c);删除指定的Collection中和本Collection中相同的元素

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.               // 将c1中与c2相同的元素删除  
  2. c1.removeAll(c2);  
  3. System.out.println(c1);  
  4. // 输出结果是[abcd1, abcd2, abcd5]  
   方法c:void clear();直接将集合中的元素清空

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.              // 3、清空元素  
  2. coll.clear();  
  3. System.out.println(coll);  
  4. // 输出的是[]  

3、判断方法:

   方法a: boolean contains(object e);  是否包含指定元素

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.               // 4、判断是否包含指定元素  
  2. Boolean b=coll.contains("abc1");  
  3. System.out.println("b="+b);  
  4. // 输出的是b=true  
  5. Boolean d=coll.contains("abc2");  
  6. System.out.println("d="+d);  
  7. // 输出的是d=false  
   方法b:boolean containsAll(Collection c); 是否包含指定容器中的元素
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 3、判断是否包含指定容器中的元素  
  2. Boolean b=c1.containsAll(c2);  
  3. System.out.println("b="+b);  
  4. // 输出的是b=true  
  方法c:Boolean isEmpty();  判断是否有元素
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 5、判断是否元素  
  2. Boolean m=coll.isEmpty();  
  3. System.out.println("m="+m);  
  4. // 输出的是m=true: 表示没有元素  
4、获取元素个数的方法:

   int size(); 获取元素的个数

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.               // 获取元素的个数  
  2. System.out.println("size="+c1.size());  
  3. // 输出的是size=8  
5、取交集的方法:

   Boolean retainAll(Collection c);保留和指定collection集合中相同的元素,不相同的元素会被删除

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.               // 5、 取交集方法  
  2. c1.retainAll(c2);  // 将c1中与c2中相同的元素保留,其余的删除  
  3. System.out.println(c1);  
  4. // 输出的是[abcd3, abcd4]  

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import java.util.ArrayList;  
  2. import java.util.Collection;  
  3.   
  4. public class Main_one {  
  5.     public static void main(String[] args)  
  6.     {  
  7.         Collection coll=new ArrayList();  
  8.         collectionDemo(coll);  
  9.                   
  10.         // Collection中带All的方法  
  11.         Collection c1=new ArrayList();  
  12.         Collection c2=new ArrayList();  
  13.         collectionAllDemo(c1,c2);  
  14.     }  
  15.     public static void collectionDemo(Collection coll)  
  16.     {  
  17.         // 导包快捷键:ctrl+shift+o  
  18.         // 1、添加元素  
  19.         coll.add("abc1");  
  20.         coll.add("abc2");  
  21.         coll.add("abc3");  
  22.         System.out.println(coll);  
  23.         // 输出的是   [abc1, abc2, abc3]  
  24.           
  25.         // 2、删除元素  
  26.         coll.remove("abc2");  
  27.         System.out.println(coll);  
  28.         // 输出的是[abc1, abc3]  
  29.           
  30.         // 4、判断是否包含指定元素  
  31.         Boolean b=coll.contains("abc1");  
  32.         System.out.println("b="+b);  
  33.         // 输出的是b=true  
  34.         Boolean d=coll.contains("abc2");  
  35.         System.out.println("d="+d);  
  36.         // 输出的是d=false  
  37.           
  38.         // 3、清空元素  
  39.         coll.clear();  
  40.         System.out.println(coll);  
  41.         // 输出的是[]     
  42.           
  43.         // 5、判断是否元素  
  44.         Boolean m=coll.isEmpty();  
  45.         System.out.println("m="+m);  
  46.         // 输出的是m=true: 表示没有元素  
  47.     }  
  48.       
  49.     // Collection中带All的方法的实现  
  50.     public static void collectionAllDemo(Collection c1,Collection c2)  
  51.     {  
  52.         // 1、给这两个集合中存储一些元素  
  53.         c1.add("abcd1");  
  54.         c1.add("abcd2");  
  55.         c1.add("abcd3");  
  56.         c1.add("abcd4");  
  57.         c1.add("abcd5");  
  58.           
  59.         c2.add("abcd3");  
  60.         c2.add("abcd4");  
  61.         c2.add("abcd6");  
  62.           
  63.         System.out.println(c1);  
  64.         // 输出的是[abcd1, abcd2, abcd3, abcd4, abcd5]  
  65.           
  66.         // 5、 取交集方法  
  67.         c1.retainAll(c2);  
  68.         System.out.println(c1);  
  69.         // 输出的是  
  70.           
  71.         // 2、把c2中的元素添加到c1中  
  72.         c1.addAll(c2);  
  73.         System.out.println(c1);  
  74.         // 输出的是[abcd1, abcd2, abcd3, abcd4, abcd5, abcd3, abcd4, abcd6]  
  75.           
  76.         // 获取元素的个数  
  77.         System.out.println("size="+c1.size());  
  78.         // 输出的是size=8  
  79.                   
  80.         // 3、判断是否包含指定容器中的元素  
  81.         Boolean b=c1.containsAll(c2);  
  82.         System.out.println("b="+b);  
  83.         // 输出的是b=true  
  84.           
  85.         // 4、将c1中与c2相同的元素删除  
  86.         c1.removeAll(c2);  
  87.         System.out.println(c1);  
  88.         // 输出结果是[abcd1, abcd2, abcd5]  
  89.               
  90.     }  
  91. }  
0 0
原创粉丝点击