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. OSQL 用法

    osql中的启动参数如下: -?:列出osql各个参数的语法摘要. -U login_id:用来指出登录名称,登录名区分大小写. -P password:用来指出登录名称所对应的密码. -E:表示采用 ...

  2. 只操作git(cmd)就可以使用git将项目上传到github

    代码改变世界 使用git将项目上传到github(最简单方法) 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具, ...

  3. 【Luogu】P1280尼克的任务(DP)

    做顺序DP做惯了,死活没想到这是个倒序DP. f[i]表示时刻i的最大空闲时.有以下两种可能. 1.时刻i没有任务.此时f[i]=f[i+1]+1; 2.时刻i有许多任务.此时f[i]=max(f[i ...

  4. 【Luogu】P2912牧场散步(TarjanLCA)

    题目链接 老天……终于碰上一个除了模板之外的LCA题了 这道题用Tarjan来LCA.树上两个点的路径是唯一的,所以钦定一个根,两点间的路径就是两点到根的路径减去双倍的公共祖先到根的路径.大概很好理解 ...

  5. [luoguP2606] [ZJOI2010]排列计数(DP)

    传送门 如果能够根据题意看出这是一个堆的话,那么就有些思路了.. 首先堆顶必须是最小元素,然后左右儿子可以预处理出来都有多少个数, 把剩余的数任意分配给两个儿子,用排列组合即可 dp(now) = d ...

  6. AJAX技术简介

    AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 您应当具备的基础知识 在继续学习之前,您需要对下面的知识有基本的了解: HTML / XHTML CSS JavaScript ...

  7. json格式前端显示

    使用angular可以稍加修改. [转]http://web.jobbole.com/82865/ function output(inp) { document.body.appendChild(d ...

  8. farm

    farm 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 White Rabbit has ...

  9. Mac 10.9 安装及使用 phoneGap

    公司网速是个坑啊 绝对掉进去就起不来的那种..... 按官网的说法: 1. 下载并安装Node.js 2. sudo npm install -g phonegap  官网的安装步骤到此为止....坑 ...

  10. Scrapy学习-6-JSON数据处理

    使用json模块处理JSON数据 class JsonwithEncodingPipeline(object): def __init__(self): self.file = codecs.open ...