黑马程序员JAVA基础Java 集合之Collection 接口和遍历方法

  Java 的集合类可以用于存储数量不等的多个对象,并可以实现常用的数据结构。

  为什么出现集合类?

  因为面向对象语言对事务的提现都是以对象的形式,所以为了方便多个对象的操作,就对对象进行存储,集合就是存储对象最常用的方式。

  数组和集合类 有什么不同的?

  数组虽然也可以存储对象,但长度是固定的,集合长度是可变的;数组中可以存储基本数据类型,而且数组只能存储同一对象,集合只能存储对象,且可以存储多种不同的对象。

  集合类的特点:只用于存储对象,长度可变,存储不同的类型对象。

  

一. Collection 接口中的共性方法

  Collection 是Java 集合框架中的跟接口,其子接口有:Set、Queue、List。

  Collection 中包定义了共性方法分类为:添加、删除、判断等。

 1 public class CollectionText {
 2     public static void main(String[] args)
 3     {
 4 //        创建一个集合容器,使用Collection 接口的子类: ArrayList 
 5         Collection e = new ArrayList() ; 
 6         System.out.println("集合是不是为空:"+e.isEmpty());
 7         CollectionAddMethod(e) ; 
 8         System.out.println("集合是不是为空:"+e.isEmpty());
 9         System.out.println("----我是分割线-----");
10         CollectionRemoveMethod(e) ; 
11         System.out.println("----我是分割线-----");
12         CollectionRetainAllMethod(e) ;
13         System.out.println("----我是分割线-----");
14         CollectionContainsMethod(e);
15     }
16     public static void CollectionAddMethod(Collection e)
17     {
18 //        添加元素;
19         e.add("我是添加1") ;
20         e.add("我是添加2") ;
21         e.add("我是添加3") ;
22         e.add("我是添加4") ;
23         System.out.println("添加元素之后:");
24         System.out.println(e);
25     }
26     public static void CollectionRemoveMethod(Collection e)
27     {
28 //        删除元素:
29         e.remove("我是添加3") ;
30         System.out.println("删除元素之后:");
31         System.out.println(e);
32     }
33     public static void CollectionRetainAllMethod(Collection e)
34     {
35         Collection e1 = new ArrayList() ;  
36         e1.add("我是添加4") ; 
37         System.out.println("retainAll 之前的e : "+e);
38 //        取交集,e 中只会保留 e1 中相同的元素。
39         e.retainAll(e1) ; 
40         System.out.println("retainAll 之后的e : " + e); 
41     }
42     public static void CollectionContainsMethod(Collection e)
43     {
44         System.out.println("e有没有<我是添加4>:"+e.contains("我是添加4") ) ;
45         System.out.println("e有没有<我是添加1>:"+e.contains("我是添加1"));
46     }
47 }

  1、添加 :

   > boolean add(Object o) add方法的参数类型是Object ,以便接收任意类型的对象。

    注意:集合中存储的都是对象的引用(地址) ;

   > boolean addAll(Collection<? extends e> c)

  2、删除:

   > boolean remove(Object o) : 删除指定的对象。

   > boolean remove(Conllection<?> c)

   > void clear() 移除此 collection 中的所有元素。

  3、判断:

、  > boolean isEmpty() 。 如果此 collection 不包含元素,则返回true ; 

   > boolean contains(Object o)  如果此 collection 包含指定的元素,则返回 true。 

   > boolean containsAll(Collection<?> c) 如果此 collection 包含指定 collection 中的所有元素,则返回 true。 

  4、取交集:

   > boolean retainAll(Collection<?> c) 保留 次 collection 和 c 共同拥有的元素。

二.遍历集合元素:

  2.1 使用 Iterator 接口遍历集合元素: 

  Iterator 主要用于遍历Collection 集合中的元素,Iterator 对象也被称为迭代器。Iterator 接口里面定义了如下三个方法:

  > boolean hasNext() : 如果被迭代的集合元素还没有被遍历,这返回true 。

  > Object next() : 返回集合元素里下一个元素。

  > void remove() : 删除集合里上一次next 方法返回的元素。

 1 public class IteratorText {
 2     public static void main(String[] args)
 3     {
 4 //        创建集合类,使用Collection 子接口的实现类:ArrayList ;
 5         Collection books = new ArrayList() ; 
 6 //        添加元素、
 7         books.add("神雕侠侣") ;
 8         books.add("笑傲江湖") ;
 9         books.add("天涯明月刀")  ;
10         books.add("诛仙") ;
11 //        取得books对应的迭代器:
12         Iterator it = books.iterator() ; 
13         while(it.hasNext())
14         {
15 //            it.next() 方法返回的数据类型是Object 类型。进行强制转换
16             String book = (String) it.next() ; 
17             System.out.println(book);
18             if(book.equals("天涯明月刀")) 
19                 it.remove() ;  
20         } 
21         System.out.println(books );
22     }
23 }

   注意:当使用Iterator 来访问 Collection 集合元素时,Collection 集合里面的元素不能被改变(不可通过集合对象的方法操作集合中的元素,会发生异常)。只能通过Iterator 的方法操作元素。

  把取出方式定义在集合的内部,这样取出方式可以直接访问集合内容的元素。去除方式就被定义成内部类。每个容器的数据结构不同,所以取出的动作细节也不一样,但是都有共性内容:判断和取出

   2.2 使用foreach 循环遍历集合元素

  JDK 1.5 提供的 foreach 循环来代替访问集合元素更加便捷:

 1 public class IteratorText {
 2     public static void main(String[] args)
 3     {
 4 //        创建集合类,使用Collection 子接口的实现类:ArrayList ;
 5         Collection books = new ArrayList() ; 
 6 //        添加元素、
 7         books.add("神雕侠侣") ;
 8         books.add("笑傲江湖") ;
 9         books.add("天涯明月刀")  ;
10         books.add("诛仙") ;
11 //        使用foreach 遍历
12         for(Object obj : books)
13         {
14             String book = (String )obj ; 
15             System.out.println(book);
16         }
17         System.out.println(books);
18     }
19 }
原文地址:https://www.cnblogs.com/jbelial/p/2981395.html