zoukankan      html  css  js  c++  java
  • Java集合----概述、Collection接口、Iterator接口

    Java 集合概述


    Java 集合就像一种容器,可以把多个对象的引用放入容器中。

    Java 集合类可以用于存储数量不等的多个对象,还可用于保存具有映射关系的关联数组

    Java 集合可分为 Set、List 和 Map 三种体系
      Set无序、不可重复的集合
      List有序,可重复的集合
      Map:具有映射关系的集合

    在 Java5 之前,Java 集合会丢失容器中所有对象的数据类型,把所有对象都当成 Object 类型处理;从 Java5 增加了泛型以后,Java 集合可以记住容器中对象的数据类型。

    Collection 接口


    Collection 接口是 List、Set 和 Queue 接口的父接口,该接口里定义的方法既可用于操作 Set 集合,也可用于操作 List 和 Queue 集合:

     

      1 public class TestCollections {
      2     public static void main(String[] args) {
      3         
      4         //1. 创建一个 Collection 接口的对象. 
      5         Collection collection = new ArrayList();
      6     
      7         //2. Collection 重要方法说明: 
      8         
      9         /**
     10          * 2.1 用于添加元素的:
     11          * add()
     12          * addAll()
     13          */
     14         Person p1 = new Person();
     15         collection.add(p1);
     16         collection.add(new Person());
     17         
     18         Collection collection2 = new ArrayList();
     19         collection2.add(new Person());
     20         collection2.add(new Person());
     21 
     22         collection.addAll(collection2);
     23         
     24         System.out.println(collection.size()); 
     25         
     26         /**
     27          * 2.2 用于访问集合的方法: 
     28          * 获取集合的长度: size()
     29          * 对集合进行遍历的方法: iterator() 可以得到对应的 Iterator 接口对象. 
     30          * 
     31          * Iterator: 迭代器
     32          * ①. 获取 Iterator 接口对象: 
     33          * ②. 使用 while 循环和 Iterator 对象遍历集合中的每一个元素. 具体使用 Iterator 接口的
     34          *    hasNext() 和 next() 方法. 
     35          */
     36         Iterator iterator = collection.iterator();
     37         
     38         while(iterator.hasNext()){
     39             Object obj = iterator.next();
     40             System.out.println(obj); 
     41         }
     42         
     43         /**
     44          * 2.3 移除集合中的元素: 
     45          * remove(): 移除某一个指定的对象. 通过 equals() 方法来判断要移除的那个元素在集合中是否存在. 以及是否能够成功移除. 
     46          * removeAll()
     47          * clear(): 使集合中的元素置空. 
     48          */
     49 //        collection.clear();
     50         
     51 //        boolean result = collection.remove(p1);
     52 //        System.out.println(result); 
     53 //        
     54 //        result = collection.removeAll(collection2);
     55 //        
     56 //        System.out.println(collection.size()); 
     57         
     58         /**
     59          * 2.4 用于检测集合的方法
     60          * retains()
     61          * retainsAll()
     62          * isEmpty()
     63          * 
     64          */
     65         System.out.println(collection.contains(new Person()));//false
     66         System.out.println(collection.contains(p1));//true
     67         System.out.println(collection.containsAll(collection2));//true
     68         
     69         System.out.println(collection.isEmpty()); //false
     70 //        collection.clear();
     71         System.out.println(collection.isEmpty()); //true
     72         
     73         /**
     74          * 2.5 其他方法
     75          * toArray(): 返回这个集合对应的数组对象
     76          * **T [] toArray(T[]): 涉及到泛型, 后面再说. 
     77          * 
     78          * equals(): 比较两个集合是否相等. 
     79          * hasCode(): 
     80          * 
     81          */
     82         Object [] objs = collection.toArray();
     83         System.out.println(objs.length); //4
     84         
     85         Person p2 = new Person();
     86         
     87         Collection collection3 = new HashSet();
     88         collection3.add(p1);
     89         collection3.add(p2);
     90         
     91         Collection collection4 = new HashSet();
     92         collection4.add(p2);
     93         collection4.add(p1);
     94         
     95         System.out.println(collection3.equals(collection4)); 
     96         
     97         /**
     98          * 使用增强 for 循环的方式来对集合进行遍历
     99          */
    100         for(Object obj: collection){
    101             System.out.println(obj); 
    102         }
    103     }
    104 }

    Iterator 接口


    Iterator 接口主要用于遍历 Collection 集合中的元素,Iterator 对象也被称为迭代器

    Iterator 接口隐藏了各种 Collection 实现类的底层细节,向应用程序提供了遍历 Collection 集合元素的统一编程接口

    Iterator 仅用于遍历集合,Iterator 本身并不提供承装对象的能力。如果需要创建 Iterator 对象,则必须有一个被迭代的集合

  • 相关阅读:
    降低大气分
    99999
    88888
    77777
    HandlerThread实现原理
    Android 内存泄漏总结
    Handler实现机制,同步屏障,IdleHandler
    launcher 配置
    微信小程序 上传图片七牛
    微信小程序 跳转传参数 传对象
  • 原文地址:https://www.cnblogs.com/justdoitba/p/7234387.html
Copyright © 2011-2022 走看看