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。

Iterator && Iterable Collection && Map的更多相关文章

  1. java Iterator Iterable Collection AbstractCollection Map关系

    java.lang Interface Iterable<T>  实现该接口就可以使用for-each循环. java.util Interface Iterator<E>   ...

  2. 理解java容器:iterator与collection,容器的起源

    关于容器 iterator与collection:容器的起源 iterator的简要介绍 iterable<T> iterator<T> 关于remove方法 Collecti ...

  3. Collection Map Java数据结构

    Collection Map 框架图 Collection          接口的接口   对象的集合 ├ List                   子接口      按进入先后有序保存   可 ...

  4. 模块导入from collections import Iterator,Iterable失败

    1.引入模块报错 from collections import Iterator,Iterable 报错: DeprecationWarning: Using or importing the AB ...

  5. Java容器类接口:Iterator,Collection,Map

    Iterator Iterator被称为迭代器,是一个对象,它的工作是遍历并选择序列中的对象,可以实现以下一些操作: 使用方法iterator()要求容器返回一个Iterator,Iterator将返 ...

  6. 初识Java集合框架(Iterator、Collection、Map)

    1. Java集合框架提供了一套性能优良.使用方便的接口和类,它们位于java.util包中 注意: 既有接口也有类,图中画实线的是类,画虚线的是接口 使用之前须要到导入java.util包 List ...

  7. iterator & iterable

    一. java.lang.Iterable java.util.Iterator Iterator是迭代器类,而Iterable是接口. 好多类都实现了Iterable接口,这样对象就可以调用iter ...

  8. Collection&Map

    1.Collection 添加元素 boolean add(E e) 删除元素 boolean remove(E e) 元素个数 int size() 清空 void clear() 判空 boole ...

  9. 基础常用的数据结构 Collection Map

    map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等.其中这四者的区别如下(简单介绍): HashMap:我们最常用的Map ...

随机推荐

  1. Thread.getContextClassLoader() is null

    Java threads created from JNI code in a non-java thread have null ContextClassloader unless the crea ...

  2. Mahout0.9安装与配置(完全分布式模式下运行)

    安装Mahout之前,一定要把hadoop装好,hadoop的安装方法可以参考我的前一篇随笔,我安装的是hadoop2.7.0,具体方法在此不做介绍. 1.首先下载相应版本的Mahout: axel ...

  3. 刷题总结——动态逆序对(bzoj3295)

    题目: Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素 ...

  4. 使用HttpClient实现对第三方服务器的请求并接受返回数据

    /* * 创建日期 2017-4-7 * * TODO 要更改此生成的文件的模板,请转至 * 窗口 - 首选项 - Java - 代码样式 - 代码模板 */ package com.enfo.int ...

  5. 【Tyvj1982】武器分配(费用流)

    题意:有N个人要从A个物品中各取一个,B个物品中各取一个,选取第i个A类物品和第j个B类物品的费用是(a[i]-b[j])^2 求最小总花费 n<=a,b<=80 a[i],b[i]< ...

  6. HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化

    转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 A ...

  7. 洛谷—— P2515 [HAOI2010]软件安装

    题目描述 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大). 但是 ...

  8. CODEVS_1227 方格取数2 网络流 最小费用流 拆点

    原题链接:http://codevs.cn/problem/1227/ 题目描述 Description 给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现在从(1, ...

  9. 心脏滴血漏洞复现(CVE-2014-0160)

    漏洞范围: OpenSSL 1.0.1版本 漏洞成因: Heartbleed漏洞是由于未能在memcpy()调用受害用户输入内容作为长度参数之前正确进 行边界检查.攻击者可以追踪OpenSSL所分配的 ...

  10. VS2017不能生成Database Unit Test项目

    问题描述: VS2017生成Database Unit Test项目时,报出如下错误,但该项目在VS2015中能正常生成: 主要是因为下面两个程序集找不到引用: Microsoft.Data.Tool ...