Java集合类库将集合的接口与实现分离。同样的接口,可以有不同的实现。

Java集合类的基本接口是Collection接口。而Collection接口必须实现Iterable接口。

以下图表示集合框架的接口,java.lang以及java.util两个包里的。红色字体部分是OCJP考纲要求的接口。其他部分可以从左向右看,比如Collection的Subinterfaces有List,Set以及Queue等。

Iterator接口

Iterator接口包含三个方法:

[java] view
plain
copy

  1. public interface Iterator<E>{
  2. E next();
  3. boolean hasNext();
  4. void remove();
  5. }

以下例子是利用了Iterator接口的着三个方法,实现遍历ArrayList<String>类型。

  • 一开始迭代器在所有元素的左边,调用next()之后,迭代器移到第一个和第二个元素之间,next()方法返回迭代器刚刚经过的元素。
  • hasNext()若返回True,则表明接下来还有元素,迭代器不在尾部。
  • remove()方法必须和next方法一起使用,功能是去除刚刚next方法返回的元素。
[java] view
plain
copy

  1. package com.xujin;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5. public class Test{
  6. public static void main(String...arg){
  7. Collection<String> a = new ArrayList<String>();
  8. a.add("Bob");
  9. a.add("Alice");
  10. a.add("Lisy");
  11. Iterator<String> iter = a.iterator();
  12. while(iter.hasNext()){
  13. String ele = iter.next();
  14. System.out.print(ele + "  ");//Bob  Alice  Lisy
  15. }
  16. System.out.println();
  17. System.out.println(a);//[Bob, Alice, Lisy]
  18. Iterator<String> iter2 = a.iterator();
  19. iter2.next();
  20. iter2.remove();
  21. System.out.println(a);//[Alice, Lisy]
  22. }
  23. }

Iterable接口

Iterable接口仅包含一个方法:

[java] view
plain
copy

  1. public interface Iterable<E>{
  2. Iterator<E> iterator();
  3. }

for-each循环可以与任何实现了Iterable接口的对象一起工作。

而Collection接口扩展了Iterable接口,故标准类库中的任何集合都可以使用for-each循环。

Collection接口

此接口的方法

public interface Collection<E>{......}

 
Modifier and Type Method and Description
boolean add(E e)

Ensures that this collection contains the specified element (optional operation).
boolean addAll(Collection<?
extends E> c)

Adds all of the elements in the specified collection to this collection (optional operation).
void clear()

Removes all of the elements from this collection (optional operation).
boolean contains(Object o)

Returns true if this collection contains the specified element.
boolean containsAll(Collection<?> c)

Returns true if this collection contains all of the elements in the specified collection.
boolean equals(Object o)

Compares the specified object with this collection for equality.
int hashCode()

Returns the hash code value for this collection.
boolean isEmpty()

Returns true if this collection contains no elements.
Iterator<E> iterator()

Returns an iterator over the elements in this collection.
boolean remove(Object o)

Removes a single instance of the specified element from this collection, if it is present (optional operation).
boolean removeAll(Collection<?> c)

Removes all of this collection's elements that are also contained in the specified collection (optional operation).
boolean retainAll(Collection<?> c)

Retains only the elements in this collection that are contained in the specified collection (optional operation).
int size()

Returns the number of elements in this collection.
Object[] toArray()

Returns an array containing all of the elements in this collection.
<T> T[] toArray(T[] a)

Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

因为其中有一个返回值为Iterator<E>类型的iterator()方法,所以,Collection接口必须实现Iterator接口

实现Collection接口的每一个类都要实现以上众多方法,但开发者自己实现很麻烦。所以java提供了AbstractCollection类来编写具体的类。

以下类都实现了Collection接口:

AbstractCollection, AbstractList, AbstractQueue, AbstractSequentialList, AbstractSet, ArrayBlockingQueue, ArrayDeque, ArrayList, AttributeList, BeanContextServicesSupport, BeanContextSupport, ConcurrentLinkedDeque,ConcurrentLinkedQueue, ConcurrentSkipListSet, CopyOnWriteArrayList, CopyOnWriteArraySet, DelayQueue, EnumSet, HashSet, JobStateReasons, LinkedBlockingDeque, LinkedBlockingQueue, LinkedHashSet, LinkedList,LinkedTransferQueue, PriorityBlockingQueue, PriorityQueue, RoleList, RoleUnresolvedList, Stack, SynchronousQueue, TreeSet, Vector

Collection接口有三个常用的子接口,分别是List,Set,Queue。

最新文章

  1. ftp协议详解
  2. select值的获取及修改
  3. ping 以及 traceroute 用法
  4. CABAC
  5. BaseAdapter 注意的关键点!
  6. 转载:C# Office 开发
  7. linux 添加定时任务脚本
  8. ImageView及其子类(三)
  9. Java中的锁——Lock和synchronized
  10. Windows程序设计:格式化对话框的设计
  11. swoole扩展实现真正的数据库连接池
  12. Anaconda管理Python环境
  13. python之第三方模块安装
  14. Flask web开发之路七
  15. Lucene整理--索引的建立
  16. 【Python】生成器和迭代器
  17. (转)Android开发--常用的传感器总结
  18. jquery双日历日期选择器bootstrap-daterangepicker日历插件
  19. P4345 [SHOI2015]超能粒子炮&#183;改 Lucas
  20. UR官网特效

热门文章

  1. 【UML】关联、依赖、泛化、实现等关系说明
  2. 九度oj 题目1159:坠落的蚂蚁
  3. 九度oj 题目1025:最大报销额
  4. Java&amp;Android代码规范
  5. 启动第一个 KVM 虚机
  6. 自定义Navigation按钮及Title
  7. elasticsearch入门使用(一)es 6.2.2安装,centos 7
  8. hdu1569 方格取数 求最大点权独立集
  9. Oracle 12c安装报错Installation failed to access the temporary location(无法访问临时位置)
  10. Codeforces 959 D Mahmoud and Ehab and another array construction task