1、 Iterable 与 Iterator

Iterable 是个接口,实现此接口使集合对象可以通过迭代器遍历自身元素.

public interface Iterable<T>

修饰符和返回值 方法名 描述
Iterator<T> iterator() 返回一个内部元素为T类型的迭代器
default void forEach(Consumer<? super T> action) 对内部元素进行遍历,并对元素进行指定的操作
default Spliterator<T> spliterator() 创建并返回一个可分割迭代器

第一个接口iterator()是jdk1.5引入的,需要子类实现一个内部迭代器Iterator遍历元素。

后两个接口是JDK1.8后新添加的,forEach(Consumer action)是为了方便遍历操作集合内的元素,spliterator()则提供了一个可以并行遍历元素的迭代器,以适应现在cpu多核时代并行遍历的需求.

其中我们可以看下default修饰符,这也是Java 8后新出现的,我们知道,如果我们给一个接口新添加一个方法,那么所有他的具体子类都必须实现此方法,为了能给接口拓展新功能,而又不必每个子类都要实现此方法,Java 8新加了default关键字,被其修饰的方法可以不必由子类实现,并且由dafault修饰的方法在接口中有方法体,这打破了Java之前对接口方法的规范.

Iterator 为迭代器类,用于遍历容器。

public interface Iterator<E>

修饰符和返回值 方法名 描述
boolean hasNext() 判断迭代器所指元素是否为最后一个
E next() 下一个元素
default Void remove() 移除迭代器只想的当前元素
default Void forEachRemaining(Consumer<? super E> action) 遍历迭代器指向元素后面的所有元素

AbstartList中实现了Iterator类作为List内部的迭代器,用于访问内部元素,其代码如下:

 private class Itr implements Iterator<E> {
/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0; /**
* Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1; /**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int expectedModCount = modCount; public boolean hasNext() {
return cursor != size();
} public E next() {
checkForComodification();
try {
int i = cursor;
E next = get(i);
lastRet = i;
cursor = i + 1;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
} public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification(); try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
} final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}

2、Collection

Collection 是容器类的接口,里面可以存放很多Elements,很多容器都实现了该接口。

public interface Collection<E> extends Iterable<E>

修饰符和返回值 方法名 描述
int size() 判断容器的大小
boolean isEmpty() 判空
boolean contains(Object o) 是否包含某元素
Object[] toArray() 转化为数组
<T> T[] toArray(T[] a) 将容器中所有元素拷贝到a中,如果a不够大,则拷贝到返回的数组中。(见AbstactCollection中实现)
boolean add(E e) 添加元素
boolean remove(Object o) 移除容器中第一个出现Object对象,如果Object为null,则移除第一个出现的null。如果没有Object对象返回false
boolean containsAll(Collection<?> c) 包含c中所有元素
boolean addAll(Collection<? extends E> c); 将c中所有元素添加到容器中
boolean removeAll(Collection<?> c) 如果容器中包含c中的元素,删除。(调用remove(Object))
default boolean removeIf(Predicate<? super E> filter) 移除所有符合条件的元素(JDK1.8中引入)
boolean retainAll(Collection<?> c) 保留在c中出现的元素,其它全部移除
boolean clear()  
boolean equals(Object o) 与o中所有元素都相等则相等
int  hashCode() c1.equals(c2) 那么他们的hashCode()一定相等,反之不成立 
default Spliterator<E> spliterator() 在所有元素之上创建一个Spliterator
default Stream<E> stream()  
default Stream<E> parallelStream()  

上述很多函数的实现可以参考 AbstactList中的实现。

3. List

List是一个接口,继承了Collection中的所有接口,并且添加了自身相关接口和具体实现。

由上图可以看出,Collection分成了三个分支,List就是其中一个,下面我们具体分析一下增加的接口。

public interface List<E> extends Collection<E>

修饰符和返回值 方法名 描述
  Collection中的所有接口  
default void  replaceAll(UnaryOperator<E> operator) 将运算操作后的结果替换List中原有元素
default void sort(Comparator<? super E> c) 将List中所有元素排序
get(int index);  
set(int index, E element)  
remove(int index)  
int indexOf(Object o) o在List中第一次出现的位置
int lastIndexOf(Object o) o在List中最后一次出现的位置
ListIterator<E> listIterator() 获取List的迭代器
ListIterator<E> listIterator(int index) 获取从某个位置开始的迭代器,index为迭代器起始位置
List<E> subList(int fromIndex, int toIndex) 子List

上表可以看出,增加了 和index相关的接口,根据index对List进行的get、set、remove等操作。另一类添加的接口是ListIteator相关的,获取List的迭代器。

3.1 ListIterator

public interface ListIterator<E> extends Iterator<E>

ListIterator 不光包含Iterator中的三个接口还增加了作为一个List迭代器应该有的接口,如 next(),previous()等

修饰符和返回值 方法名 描述
  Iterator中的所有接口  
boolean  hasNext()  
E next()  
boolean  hasPrevious()  
E previous()  
int nextIndex()  
int previousIndex()  
void  remove() 删除next()返回元素
void  set(E e) 替换next()返回的元素
void  add(E e) 在nextIndex()后插入元素

首先需要明确ListIterator迭代器的作用:

  1. 允许我们向前、向后两个方向遍历 List;
  2. 在遍历时修改 List 的元素;
  3. 遍历时获取迭代器当前游标所在位置。
注意,迭代器 没有当前所在元素一说,它只有一个游标( cursor )的概念,这个游标总是在元素之间,比如这样:

迭代器的初始位置:

调用next()后迭代器的位置

调用 previous() 游标就会回到之前位置。当向后遍历完元素,游标就会在元素 N 的后面

也就是说长度为 N 的集合会有 N+1 个游标的位置。

3.2 AbstractCollection / AbstractList

在上面的继承关系图中并没有显示出AbstractCollect和AbstractList的存在,但是在阅读源码的时候经常遇到这两个类,下面讲一下这两个类的作用。

首先需要明确的一点是AbstractCollect和AbstractList都是抽象类而不是接口,它们分别实现了Collection和List接口中的部分函数。这样继承者直接继承AbstractXXXX 而不需要自己重复实现里面的所有接口。这两个抽象类抽离了公共部分,进行了实现,减少后续类的工作量。

ArrayList 和LinkedList都直接或者间接继承了AbstartList类。下图为这两个类的继承关系:

具体这两个类里面实现了哪些接口,请自己阅读源码不再讲解。

LinkedList中modCount和expectedModCount作用

在AbstractList中可以看到这两个变量,它们用于表示List被修改的次数,这其中包括了调用集合本身的add等方法等修改方法时进行的修改和调用集合迭代器的修改方法进行的修改。而expectedModCount则是表示迭代器对集合进行修改的次数。

那么知道这两个值有什么作用呢?

如果创建多个迭代器对一个集合对象进行修改的话,那么就会有一个modCount和多个expectedModCount,且modCount的值之间也会不一样,这就导致了moCount和expectedModCount的值不一致,从而产生异常。

https://www.cnblogs.com/zhangcaiwang/p/7131035.html

3.3 ArrayList

public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable我们看到ArrayList的定义的时候很可能会出现一个疑问,为什么它已经继承了AbstractList了还需要去实现List<E>接口? 答案是,后面的List<E>可以去掉,这里只是让阅读者明白ArrayList是个List。 终于好不容易到了开发者常用的类了,有点窃喜:transient Object[] elementData;这个很重要的成员变量elementData数组用来存放ArrayList中的元素,当第一个元素添加进来后,它的默认长度变为10。(java中“transient”关键字修饰的成员变量在类序列化的过程中不会被持久化到文件中,保证该成员变量保存信息的安全。)
ArrayList的构造函数中可以直接指定elementData数组的长度,那么问题来了,当数组已经完全被占用再向ArrayList中添加元素时,如何再分配更大长度的数组?如何把旧数据拷贝到新数组中?答案见下面这段源码

 private void grow(int minCapacity) {  //最少需要的容量
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);   // 分配最小容量的 1.5倍
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity); // 最大容量比较(Inter.MAX_VALUE)
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);  // 将旧数据拷贝到新数组中
}

其它接口不再细说。

3.4 LinkedList

 public class LinkedList<E>
extends

AbstractSequentialList<E>

     implements List<E>, Deque<E>, Cloneable, java.io.Serializable

作为一个链表类型,首先需要熟悉一下节点类:Node(LinkedList静态内部类)

 private static class Node<E> {
E item;
Node<E> next;
Node<E> prev; Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}

在LinkedList中有三个主要成员变量:

  • transient int size = 0;      // 链表长度
  • transient Node<E> first;  // 链表的首节点
  • transient Node<E> last;  // 链表的末节点

LinkedList构造方法:

  /**
* Constructs an empty list.
*/
public LinkedList() {
} /**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}

clear()方法:遍历LinkedList 置null

 public void clear() {
// Clearing all of the links between nodes is "unnecessary", but:
// - helps a generational GC if the discarded nodes inhabit
// more than one generation
// - is sure to free memory even if there is a reachable Iterator
for (Node<E> x = first; x != null; ) {
Node<E> next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
first = last = null;
size = 0;
modCount++;
}

public E set(int index, E element):将某个Index指向的Node置变为element

public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index); //node(Int i) 遍历LinkedList查找第i个Node
E oldVal = x.item;
x.item = element;
return oldVal;
}

public void add(int index, E element):在链表的index位置添加一个节点

这里还有众多的插入删除等函数,其主要思想还是对链表的操作(插入删除等),详情阅读源码。

3.5 ArrayList 与LinkedList 性能比较

两种List的存储结构决定了两者的性能:ArrayList以数组形式存储,LinkedList以链表形式存储

ArrayList是以数组的形式存储的,所以他的遍历很快,可以根据index很快找到对应元素。但是,ArrayList的插入和删除操作较慢,如果需要在数组中插入一个元素或者删除一个元素较麻烦。

LinkedList的索引操作较慢(需要遍历列表才能找到对应索引元素),但是它的插入和删除操作效率高(只需要控制前后指针就能实现插入和删除)。

参考:

https://www.cnblogs.com/bushi/p/6647006.html

https://www.jianshu.com/p/047e33fdefd2

java容器类分析:Collection,List,ArrayList的更多相关文章

  1. 【转】java 容器类使用 Collection,Map,HashMap,hashTable,TreeMap,List,Vector,ArrayList的区别

    原文网址:http://www.360doc.com/content/15/0427/22/1709014_466468021.shtml java 容器类使用 Collection,Map,Hash ...

  2. java 容器(collection)--ArrayList 常用方法分析 源码分析

    ArrayList 介绍 打开jdk源码看看官方文档的介绍 粗糙的翻译下大致意思是: List接口的可调整大小的数组实现.实现了所有可选的列表操作,并允许所有元素,包括 null .除了实现List接 ...

  3. java集合框架collection(2)ArrayList和LinkedList

    ArrayList是基于动态数组实现的list,而LinkedList是基于链表实现的list.所以,ArrayList拥有着数组的特性,LinkedList拥有着链表的特性. 优缺点 ArrayLi ...

  4. Java集合框架Collection(1)ArrayList的三种遍历方法

    ArrayList是java最重要的数据结构之一,日常工作中经常用到的就是ArrayList的遍历,经过总结,发现大致有三种,上代码: package com.company; import java ...

  5. java容器类3:set/HastSet/MapSet深入解读

    介绍 Set:集合,是一个不包含重复数据的集合.(A collection that contains no duplicate elements. ) set中最多包含一个null元素,否者包含了两 ...

  6. java容器类1:Collection,List,ArrayList,LinkedList深入解读

    1. Iterable 与 Iterator Iterable 是个接口,实现此接口使集合对象可以通过迭代器遍历自身元素. public interface Iterable<T> 修饰符 ...

  7. Java容器类List、ArrayList、Vector及map、HashTable、HashMap的区别与用法

    Java容器类List.ArrayList.Vector及map.HashTable.HashMap的区别与用法 ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数 ...

  8. java基础解析系列(十)---ArrayList和LinkedList源码及使用分析

    java基础解析系列(十)---ArrayList和LinkedList源码及使用分析 目录 java基础解析系列(一)---String.StringBuffer.StringBuilder jav ...

  9. JAVA之旅(十八)——基本数据类型的对象包装类,集合框架,数据结构,Collection,ArrayList,迭代器Iterator,List的使用

    JAVA之旅(十八)--基本数据类型的对象包装类,集合框架,数据结构,Collection,ArrayList,迭代器Iterator,List的使用 JAVA把完事万物都定义为对象,而我们想使用数据 ...

随机推荐

  1. JQuery 纵向二级菜单与对齐方式

    1.效果: 2.代码: style部分: <style type="text/css"> /* ul{margin: 0; padding: 0;}*/ ul{list ...

  2. oracle plsql exception例外

    以下plsql程序用的scott用户的dept,emp表. not_data_found例外: --系统列外 set serveroutput on declare pename emp.ename% ...

  3. nongsanli

    之后的内容只能追加,不可以修改,删除. 1.    mysql可以对字段进行MD5加密, 加密插入:INSERT INTO t_user(id,username,PASSWORD) VALUES('5 ...

  4. CentOS安装scp命令

    scp这东西应该属于openssh-clients这个包,运行: yum -y install openssh-clients 再运行scp就可以了,再次运行: .txt 注意,scp 命令操作的两端 ...

  5. dlib下训练自己的物体检测器--手的检测

    之前我们在Linux上安装了dlib(http://www.cnblogs.com/take-fetter/p/8318602.html),也成功的完成了之前的人脸检测程序, 今天我们来一起学习怎样使 ...

  6. POJ 2409 Let it Bead [置换群 Polya]

    传送门 题意:$m$种颜色$n$颗珠子,定义旋转和翻转两种置换,求不等价着色数 暴力求每个置换的循环节也许会$T?$ 我们可以发现一些规律: 翻转: $n$为奇数时每个置换有$1+\frac{n-1} ...

  7. SDN第五次上机作业

    作业链接 1.建立拓扑,并连接上ODL控制器. 2.利用ODL下发组表.流表,实现建议负载均衡 查看s2接收的数据包都被drop掉了 在s1中下发组表 在s1中下发流表使组表生效 下发流表覆盖S2中d ...

  8. [Python Study Notes]字典操作

    字典操作 a.增加 >>> info["stu1104"] = "abc" >>> info {'stu1102': 'x5 ...

  9. (转)CocoaPods:管理Objective-c 程序中各种第三方开源库关联

    在我们的iOS程序中,经常会用到多个第三方的开源库,通常做法是去下载最新版本的开源库,然后拖拽到工程中. 但是,第三方开源库的数量一旦比较多,版本的管理就非常的麻烦.有没有什么办法可以简化对第三方库的 ...

  10. css去除ios文本框默认圆角

    css去除ios文本框默认圆角 input, textarea {-webkit-appearance: none;}