JAVA基础 之 Collection

来源:互联网 发布:js设置div属性 编辑:程序博客网 时间:2024/06/07 02:32

概述:

    1.Collection java集合框架的根级接口(root interface)

    2.常用子接口:List、Set、Queue,注意map是自成体系的

    3.方法:新增、包含、遍历、交集、判空、大小、清空等

 

Java代码 复制代码 收藏代码
  1. package com.cxy.collection;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.Collection;   
  5. import java.util.HashSet;   
  6. import java.util.Iterator;   
  7.   
  8. /**  
  9.  * @author chenxiaoyang  
  10.  */  
  11. public class CollectionTest   
  12. {   
  13.     /**  
  14.      * 说明:  
  15.      * 1.Collection java集合框架的根级接口(root interface) 
  16.      * 2.常用子接口:List、Set、Queue,注意map是自成体系的  
  17.      * 3.方法:新增、包含、遍历、交集、判空、大小、清空等  
  18.      */  
  19.     public static void main(String[] args)   
  20.     {   
  21.         Collection children=new ArrayList(); //注意由于这里没有加泛型,所以很多黄线警告  
  22.         //新增   
  23.         children.add("小明");   
  24.         children.add("小红");   
  25.         children.add("小白");   
  26.         System.out.println("======================");   
  27.            
  28.         //是否包含   
  29.         System.out.println("幼儿园一班是否有叫小明的小朋友? 答:"+children.contains("小明"));   
  30.         System.out.println("幼儿园一班是否有叫小黑的小朋友? 答:"+children.contains("小黑"));   
  31.         System.out.println("======================");   
  32.            
  33.            
  34.         //遍历(2种方式)   
  35.         System.out.print("Iterator法遍历:");   
  36.         Iterator it = children.iterator(); //Iterator(迭代器) 请参看博客中有关Iterator专门的文章  
  37.         while (it.hasNext())   
  38.         {   
  39.             System.out.print((String)it.next()+"  ");//由于没有使用泛型,所以这里需要强转一下  
  40.         }   
  41.         System.out.println("");  //保持格式,无实际用处  
  42.            
  43.         /*上面这种方式太注重遍历过程的本身,对初学者来说有些复杂,那么试试foreach吧 
  44.          *foreach是java 5 提供的一种便捷遍历方法  
  45.          */  
  46.         System.out.print("foreach法遍历:");   
  47.         for(Object one : children)   
  48.         {   
  49.             System.out.print((String)one+"  ");   
  50.         }   
  51.         System.out.println("");  //保持格式,无实际用处  
  52.         System.out.println("======================");   
  53.            
  54.            
  55.         //转换成数组   
  56.         Object[] array=children.toArray();   
  57.         System.out.println("数组大小:"+array.length);   
  58.         System.out.println("======================");   
  59.            
  60.         //删除   
  61.         System.out.println("删除前:"+children); //这种打印方法实际用的是Collection实现类的toString方法  
  62.         children.remove("小明");   
  63.         System.out.println("删除后:"+children);   
  64.         System.out.println("======================");   
  65.            
  66.         //交集   
  67.         Collection goodBoySet=new HashSet();  //一个set集合  
  68.         goodBoySet.add("小明");   
  69.         goodBoySet.add("小白");   
  70.         children.retainAll(goodBoySet);  //children集合中存在于goodBoySet集合的数据,简单的讲就是交集。  
  71.         System.out.println("交集结果:"+children);   
  72.         System.out.println("======================");   
  73.            
  74.         //判空、大小、清空   
  75.         System.out.println("集合是否是空?答:"+children.isEmpty());   
  76.         System.out.println("集合大小:"+children.size());   
  77.         children.clear();   
  78.         System.out.println("清空后,集合是否是空?答:"+children.isEmpty());   
  79.         System.out.println("清空后,集合大小:"+children.size());   
  80.         System.out.println("======================");   
  81.     }   
  82. }  
原创粉丝点击