集合框架之Collection

来源:互联网 发布:窃听风暴知乎 编辑:程序博客网 时间:2024/05/16 05:22

Collection是最基本的集合接口,一个Collection代表一组Object。一些 Collection允许相同的元素而另一些不行。一些能排序而另一些不行。Java SDK不提供直接继承自Collection的类,Java SDK提供的类都是继承自Collection的“子接口”如List和Set。

所以实现Collection接口的类都必须提供两个标准的构造函数:
无参的构造函数用于创建一个空的Collection
有一个Collection参数的构造函数用于创建一个新的Collection,这个新的Collection与传入Collection有相同的元素,后一个构造函数允许用户复制一个Collection

如何遍历Collection的每一个元素?
不论Collection的实际类型如何,他都支持一个iterator()的方法,该方法返回一个迭代子,使用该迭代子即可逐一访问Collection的每一个元素。
典型的用法如下:

Iterator it = collection.iterator(); // 获得一个迭代子    while(it.hasNext()) {      Object obj = it.next(); // 得到下一个元素    }

由Collection接口派生的两个接口是List和Set。

Collection接口的常用方法
集合中主要提供查探操作、修改操作、批量操作、比较和求hash值操作的方法:

int size(); 获取集合中存储的元素个数
boolean isEmpty(); 查看集合是否为空
Iterator iterator(); 获取集合的遍历器
boolean contains(Object element); 查看集合中是否包含指定元素
Object[] toArray(); 将集合转换成数组

boolean add(Object element); 向集合中添加元素
boolean remove(Object element); 从集合中移除指定元素
boolean containsAll(Collection c); 查看集合中是否包含指定集合中的所有元素
boolean addAll(Collection c) 向集合中添加指定集合中的所有元素

boolean removeAll(Collection c) 从集合中移除在指定集合中存在的元素
boolean retainAll(Collection c) 从集合中移除不在指定集合中存在的元素
void clear(); 清空集合
boolean equals(Object o); 比较集合是否相等
int hashCode(); 获取集合的hash码

1、添加 :
 boolean add(Object o) add方法的参数类型是Object ,以便接收任意类型的对象。
 注意:集合中存储的都是对象的引用(地址) ;
 boolean addAll(Collection 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 共同拥有的元素。

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

Iterator 主要用于遍历Collection 集合中的元素,Iterator 对象也被称为迭代器。Iterator 接口里面定义了如下三个方法:
boolean hasNext() : 如果被迭代的集合元素还没有被遍历,这返回true 。
Object next() : 返回集合元素里下一个元素。
void remove() : 删除集合里上一次next 方法返回的元素。

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

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

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

使用foreach循环遍历集合元素

 public class IteratorText {     public static void main(String[] args)     { //        创建集合类,使用Collection 子接口的实现类:ArrayList ;         Collection books = new ArrayList() ;  //        添加元素、         books.add("神雕侠侣") ;         books.add("笑傲江湖") ;         books.add("天涯明月刀")  ;         books.add("诛仙") ; //        使用foreach 遍历         for(Object obj : books)         {             String book = (String )obj ;              System.out.println(book);         }         System.out.println(books);     } }

参考博客:http://www.cnblogs.com/jbelial/archive/2013/03/27/2981395.html

原创粉丝点击