Iterator && Iterable Collection && Map
Java集合类库将集合的接口与实现分离。同样的接口,可以有不同的实现。
Java集合类的基本接口是Collection接口。而Collection接口必须实现Iterable接口。
以下图表示集合框架的接口,java.lang以及java.util两个包里的。红色字体部分是OCJP考纲要求的接口。其他部分可以从左向右看,比如Collection的Subinterfaces有List,Set以及Queue等。

Iterator接口
Iterator接口包含三个方法:
- public interface Iterator<E>{
- E next();
- boolean hasNext();
- void remove();
- }
以下例子是利用了Iterator接口的着三个方法,实现遍历ArrayList<String>类型。
- 一开始迭代器在所有元素的左边,调用next()之后,迭代器移到第一个和第二个元素之间,next()方法返回迭代器刚刚经过的元素。
- hasNext()若返回True,则表明接下来还有元素,迭代器不在尾部。
- remove()方法必须和next方法一起使用,功能是去除刚刚next方法返回的元素。
- package com.xujin;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
- public class Test{
- public static void main(String...arg){
- Collection<String> a = new ArrayList<String>();
- a.add("Bob");
- a.add("Alice");
- a.add("Lisy");
- Iterator<String> iter = a.iterator();
- while(iter.hasNext()){
- String ele = iter.next();
- System.out.print(ele + " ");//Bob Alice Lisy
- }
- System.out.println();
- System.out.println(a);//[Bob, Alice, Lisy]
- Iterator<String> iter2 = a.iterator();
- iter2.next();
- iter2.remove();
- System.out.println(a);//[Alice, Lisy]
- }
- }
Iterable接口
Iterable接口仅包含一个方法:
- public interface Iterable<E>{
- Iterator<E> iterator();
- }
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<?
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的更多相关文章
- java Iterator Iterable Collection AbstractCollection Map关系
java.lang Interface Iterable<T> 实现该接口就可以使用for-each循环. java.util Interface Iterator<E> ...
- 理解java容器:iterator与collection,容器的起源
关于容器 iterator与collection:容器的起源 iterator的简要介绍 iterable<T> iterator<T> 关于remove方法 Collecti ...
- Collection Map Java数据结构
Collection Map 框架图 Collection 接口的接口 对象的集合 ├ List 子接口 按进入先后有序保存 可 ...
- 模块导入from collections import Iterator,Iterable失败
1.引入模块报错 from collections import Iterator,Iterable 报错: DeprecationWarning: Using or importing the AB ...
- Java容器类接口:Iterator,Collection,Map
Iterator Iterator被称为迭代器,是一个对象,它的工作是遍历并选择序列中的对象,可以实现以下一些操作: 使用方法iterator()要求容器返回一个Iterator,Iterator将返 ...
- 初识Java集合框架(Iterator、Collection、Map)
1. Java集合框架提供了一套性能优良.使用方便的接口和类,它们位于java.util包中 注意: 既有接口也有类,图中画实线的是类,画虚线的是接口 使用之前须要到导入java.util包 List ...
- iterator & iterable
一. java.lang.Iterable java.util.Iterator Iterator是迭代器类,而Iterable是接口. 好多类都实现了Iterable接口,这样对象就可以调用iter ...
- Collection&Map
1.Collection 添加元素 boolean add(E e) 删除元素 boolean remove(E e) 元素个数 int size() 清空 void clear() 判空 boole ...
- 基础常用的数据结构 Collection Map
map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等.其中这四者的区别如下(简单介绍): HashMap:我们最常用的Map ...
随机推荐
- 将SSM架构中原来关于springSecurity3.x版本的写法配迁移到SpringBoot2.0框架中出现的问题解决记
迁移过程中关于这个安全框架的问题很麻烦,springBoot自带的stater中的版本是5.0,原来系统有通过实现"org.springframework.security.authenti ...
- msp430项目编程37
msp430中项目---usb接口编程37 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结
- list或map 打印成json 方便调试
private final Logger logger = Logger.getLogger(this.getClass()); logger.info(JSON.toJSONStringWithDa ...
- eclipse 安卓虚拟机安装apk 及常见问题
首先必须启动虚拟机然后如图操作:
- codevs——1842 递归第一次
1842 递归第一次 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 同学们在做题时常遇到这种函数 f( ...
- IIS Express 的怪毛病 vs2013本机调试
本机调试时,如果同一个项目有多个版本,同时debug,可能会串项目调试,造成不必要的困扰: 通常情况下是 IIS express的映射出现了问题: 解决方案: 1.打开目录:查看文件C:\Users\ ...
- flask使用debug模式时,存在错误时,会占用设备内存直至服务重启才释放;debug模式会开启一个守护进程(daemon process)
函数调用顺序flask的app.py的run-->werkzeug的serving.py的run_simple-->调用werkzeug的debug的__init__.py里的类Debug ...
- k-svd字典学习,稀疏编码
1. K-SVD usage: Design/Learn a dictionary adaptively to betterfit the model and achieve sparse s ...
- linux 状态与系统调优
什么样的cup 才算是负载高呢?才算忙呢?
- 阿里云OSS 图片处理api(custom)
阿里云OSS 图片处理api(custom) 阿里云对象存储服务(Object Storage Service, 简称OSS) 学习了:https://blog.csdn.net/u014559227 ...