您的位置:首页 > 运维架构 > 网站架构

Collection架构源码分析(基于1.8)

2016-06-18 16:01 633 查看
Collection接口有三个子接口,我们主要来分析一下其中的两种:List和Set

List:有序集合,其中元素可以重复。

Set:无序集合,元素不可以重复。

List和Set两个接口都各自的抽象实现类。

Collection源码分析

源码中的API:

public interface Collection<E> extends Iterable<E>
{
int size();
boolean isEmpty()();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
<T> T[] toArray(T[] a);

//Modification Operations(修改 操作)
boolean add(E e);
boolean remove(Object o);

//Bulk Operations(块 操作)
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean removeAll(Collection<?> c);
default boolean removeIf(Predicate<? super E> filter) {};
boolean retainAll(Collection<?> c);
void clear();

//Comparison and hashing(比较 和 哈希)
boolean equals(Object o);
int hashCode();
default Spliterator<E> spliterator(){};
default Stream<E> stream(){};
default Stream<E> parallelStream(){};
}


以上是JDK1.8中Collection中的API,与JDK1.7中的源码相比的不同的是,添加了几个方法如下:

default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
final Iterator<E> each = iterator();
while (each.hasNext()) {
if (filter.test(each.next())) {
each.remove();
removed = true;
}
}
return removed;
};//移除满足特定谓词的集合,可以理解为将集合通过一个设定了某些限制条件的过滤器,然后通过该过滤器滤除集合中的某些成员
@Override
default Spliterator<E> spliterator() {//创造一个分离器,与并发处理有关,下面有方法使用示例
return Spliterators.spliterator(this, 0);
}
default Stream<E> stream() {//也是与并发处理有关---创建一个集合的操作流(单线程流)
return StreamSupport.stream(spliterator(), false);
}
default Stream<E> parallelStream() {//创建一个集合的并行操作流
return StreamSupport.stream(spliterator(), true);
}


从上面我们可以看出接口中也可以定义方法,不过属性应是default

下面是我找到的一些关于新特性的代码,大家可以测试一下:

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

/**
Parallel Streams , 并行流提高性能

流可以是顺序的也可以是并行的。顺序流的操作是在单线程上执行的,而并行流的操作是在多线程上并发执行的。
*/
public class ParallelStreams {

int max = 1000_000;
List<String> values;

public ParallelStreams(){
//创建一个包含唯一元素的大容器:
values = new ArrayList<String>();
for(int i=max; i>0; i--){
UUID uuid = UUID.randomUUID();
values.add(uuid.toString());
}
}
//测试排序这些元素需要多长时间。

//Sequential Sort, 采用顺序流进行排序
@Test
public void sequentialSort(){
long t0 = System.nanoTime();

long count = values.stream().sorted().count();
System.err.println("count = " + count);

long t1 = System.nanoTime();

long millis  = TimeUnit.NANOSECONDS.toMillis(t1 - t0);
System.out.println(String.format("sequential sort took: %d ms", millis));
//sequential sort took: 1932 ms

}

//parallel Sort, 采用并行流进行排序
@Test
public void parallelSort(){
long t0 = System.nanoTime();

long count = values.parallelStream().sorted().count();
System.err.println("count = " + count);

long t1 = System.nanoTime();

long millis  = TimeUnit.NANOSECONDS.toMillis(t1 - t0);
System.out.println(String.format("parallel sort took: %d ms", millis));
//parallel sort took: 1373 ms 并行排序所花费的时间大约是顺序排序的一半。
}
}


代码中用到了junit单元测试包,大家完全可以去掉,然后使用main方法来查看运行结果。具体关于这部分内容,推荐阅读JessenPan

其实从上面我们可以看出,1.8中增强了对内建性功能接口的功能。

List源码分析

以下是API:

public interface List<E> extends Collection<E>{
// 继承自Collection的API
boolean         add(E object)
boolean         addAll(Collection<? extends E> collection)
void            clear()
boolean         contains(Object object)
boolean         containsAll(Collection<?> collection)
boolean         equals(Object object)
int             hashCode()
boolean         isEmpty()
Iterator<E>     iterator()
boolean         remove(Object object)
boolean         removeAll(Collection<?> collection)
boolean         retainAll(Collection<?> collection)
int             size()
<T> T[]         toArray(T[] array)
Object[]        toArray()

// 相比与Collection,List新增的API:
void                add(int location, E object) //在指定位置添加元素
boolean             addAll(int location, Collection<? extends E> collection) //在指定位置添加其他集合中的元素
E                   get(int location) //获取指定位置的元素
int                 indexOf(Object object) //获得指定元素的索引
int                 lastIndexOf(Object object) //从右边的索引
ListIterator<E>     listIterator(int location) //获得iterator
ListIterator<E>     listIterator()
E                   remove(int location) //删除指定位置的元素
E                   set(int location, E object) //修改指定位置的元素
List<E>             subList(int start, int end) //获取子list

//相对于1.7的源码,1.8新增加的内容
/**
* Replaces each element of this list with the result of applying the
* operator to that element.  Errors or runtime exceptions thrown by
* the operator are relayed to the caller.
* 以上大致意思是对list中的所有元素均应用一次特定的操作。
*/
default void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);//这个方法是Objects中的静态方法,如果operator是null,那么抛出空指针异常,如果不为空,那么返回原对象。因此这个方法只是用来进行对象检查的方法。
final ListIterator<E> li = this.listIterator();
while (li.hasNext()) {
li.set(operator.apply(li.next()));
}
}

//根据比较器的比较逻辑对list进行排序。
@SuppressWarnings({"unchecked", "rawtypes"})
default void sort(Comparator<? super E> c) {
Object[] a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {//这里存在遍历时修改的疑问,待解决。
i.next();
i.set((E) e);
}
}

//重载了Collection中默认实现的方法
@Override
default Spliterator<E> spliterator() {
return Spliterators.spliterator(this, Spliterator.ORDERED);
}

}


与1.7相比,1.8中增加了3个方法且均已默认了实现。

Set接口源码分析

Set中不能有重复的元素,这是其最大的特点。

源码中,1.7中Set源码与Collection一样,但是在1.8中,Set接口有一点改动,那就是重写了一个默认的方法。

接口签名:

public interface Set<E> extends Collection<E>```


//与Collection不同的部分
@Override
default Spliterator<E> spliterator() {
return Spliterators.spliterator(this, Spliterator.DISTINCT);
}


AbstractCollection类分析

与1.7相比,1.8中这个类的实现没有任何改动。

public abstract class AbstractCollection<E> implements Collection<E> {
protected AbstractCollection() {
}

public abstract Iterator<E> iterator();//iterator()方法没有实现

public abstract int size(); //size()方法也没有实现

public boolean isEmpty() { //检测集合是否为空
return size() == 0;
}
/*检查集合中是否包含特定对象*/
public boolean contains(Object o) {
Iterator<E> it = iterator();
if (o==null) {
while (it.hasNext()) //从这里可以看出,任何非空集合都包含null
if (it.next()==null)
return true;
} else {
while (it.hasNext())
if (o.equals(it.next()))
return true;
}
return false;
}
/*将集合转变成数组*/
public Object[] toArray() {
// Estimate size of array; be prepared to see more or fewer elements
Object[] r = new Object[size()]; //创建与集合大小相同的数组
Iterator<E> it = iterator();
for (int i = 0; i < r.length; i++) {
if (! it.hasNext()) // fewer elements than expected
//Arrays.copy(**,**)的第二个参数是待copy的长度,如果这个长度大于r,则保留r的长度
return Arrays.copyOf(r, i);
r[i] = it.next();
}
return it.hasNext() ? finishToArray(r, it) : r;
}

public <T> T[] toArray(T[] a) {
// Estimate size of array; be prepared to see more or fewer elements
int size = size();
T[] r = a.length >= size ? a :
(T[])java.lang.reflect.Array
.newInstance(a.getClass().getComponentType(), size);
Iterator<E> it = iterator();

for (int i = 0; i < r.length; i++) {
if (! it.hasNext()) { // fewer elements than expected
if (a == r) {
r[i] = null; // null-terminate
} else if (a.length < i) {
return Arrays.copyOf(r, i);
} else {
System.arraycopy(r, 0, a, 0, i);
if (a.length > i) {
a[i] = null;
}
}
return a;
}
r[i] = (T)it.next();
}
// more elements than expected
return it.hasNext() ? finishToArray(r, it) : r;
}

private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
int i = r.length;
while (it.hasNext()) {
int cap = r.length;
if (i == cap) {
int newCap = cap + (cap >> 1) + 1;
// overflow-conscious code
if (newCap - MAX_ARRAY_SIZE > 0)
newCap = hugeCapacity(cap + 1);
r = Arrays.copyOf(r, newCap);
}
r[i++] = (T)it.next();
}
// trim if overallocated
return (i == r.length) ? r : Arrays.copyOf(r, i);
}

private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError
("Required array size too large");
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}

// 删除对象o
public boolean remove(Object o) {
Iterator<E> it = iterator();
if (o==null) {
while (it.hasNext()) {
if (it.next()==null) {
it.remove();
return true;
}
}
} else {
while (it.hasNext()) {
if (o.equals(it.next())) {
it.remove();
return true;
}
}
}
return false;
}
// 判断是否包含集合c中所有元素
public boolean containsAll(Collection<?> c) {
for (Object e : c)
if (!contains(e))
return false;
return true;
}

//添加集合c中所有元素
public boolean addAll(Collection<? extends E> c) {
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;//从这里可以看出,c中只要有一个元素添加成功,就可能导致返回的结果为true,而不是只有全部元素添加成功才会返回true。
return modified;
}

//删除集合c中所有元素(如果存在的话)
public boolean removeAll(Collection<?> c) {
boolean modified = false;
Iterator<?> it = iterator();
while (it.hasNext()) {
if (c.contains(it.next())) {
it.remove();
modified = true;//同上,只要有一个元素删除成功,就有可能返回true。
}
}
return modified;
}

//清空
public void clear() {
Iterator<E> it = iterator();
while (it.hasNext()) {
it.next();
it.remove();
}
}

//将集合元素显示成[String1], [String2], [String3], ......
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";

StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}

}


AbstractList源码分析

1.8与1.7相比,AbstractList类源码没有变化。

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {

protected AbstractList() {
}

public boolean add(E e) {
add(size(), e);//这里调用的下面那个add方法。
return true;
}

abstract public E get(int index);

public E set(int index, E element) {//根据索引位置值修改某个元素,这里只是抛出了一个异常,这应该就要求他的子类重写这个方法。
throw new UnsupportedOperationException();
}

public void add(int index, E element) {//同上。
throw new UnsupportedOperationException();
}

public E remove(int index) {
throw new UnsupportedOperationException();
}

/***************************** Search Operations**********************************/
public int indexOf(Object o) { //搜索对象o的索引
ListIterator<E> it = listIterator();
if (o==null) {
while (it.hasNext())
if (it.next()==null) //执行it.next(),会先返回it指向位置的值,然后it会移到下一个位置
return it.previousIndex(); //所以要返回it.previousIndex(); 关于it几个方法的源码在下面
} else {
while (it.hasNext())
if (o.equals(it.next()))
return it.previousIndex();
}
return -1;
}
//从下面的代码中我们应该注意到一种处理理念:那就是对参数进行情况的分类处理,否则可能会产生空指针异常尤其是在集合框架的源码中。以后我们会看到很多类似的处理过程。
public int lastIndexOf(Object o) {
ListIterator<E> it = listIterator(size());
if (o==null) {//null的情况
while (it.hasPrevious())
if (it.previous()==null)
return it.nextIndex();
} else {//非null的情况
while (it.hasPrevious())
if (o.equals(it.previous()))
return it.nextIndex();
}
return -1;
}
/**********************************************************************************/

/****************************** Bulk(块) Operations ***********************************/
public void clear() {
removeRange(0, size());
}

public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
boolean modified = false;
for (E e : c) {
add(index++, e);
modified = true;
}
return modified;
}

protected void removeRange(int fromIndex, int toIndex) {
ListIterator<E> it = listIterator(fromIndex);
for (int i=0, n=toIndex-fromIndex; i<n; i++) {
it.next();
it.remove();//这里是使用for循环来操作元素。关于这里也有一个值得探讨的问题,后面我可能会专门用一篇博文来总结这个问题。
}
}
/**********************************************************************************/

/********************************* Iterators **************************************/
public Iterator<E> iterator() {
return new Itr();
}

public ListIterator<E> listIterator() {
return listIterator(0); //返回的iterator索引从0开始
}

public ListIterator<E> listIterator(final int index) {
rangeCheckForAdd(index); //首先检查index范围是否正确

return new ListItr(index); //ListItr继承与Itr且实现了ListIterator接口,Itr实现了Iterator接口,往下看
}

private class Itr implements Iterator<E> {
int cursor = 0; //元素的索引,当调用next()方法时,返回当前索引的值
int lastRet = -1; //lastRet也是元素的索引,但如果删掉此元素,该值置为-1(元素是否已被删除的标示)
/*
*迭代器都有个modCount值,在使用迭代器的时候,如果使用remove,add等方法的时候都会修改modCount,
*在迭代的时候需要保持单线程的唯一操作,如果期间进行了插入或者删除,modCount就会被修改,迭代器就会检测到被并发修改,从而出现运行时异常。
*举个简单的例子,现在某个线程正在遍历一个List,另一个线程对List中的某个值做了删除,那原来的线程用原来的迭代器就无法正常遍历了
*/
int expectedModCount = modCount;//期望值与原值,感觉有点像CAS操作一样=.=

public boolean hasNext() {
return cursor != size(); //当索引值和元素个数相同时表示没有下一个元素了,索引是从0到size-1
}

public E next() {
checkForComodification(); //检查modCount是否改变
try {
int i = cursor; //next()方法主要做了两件事:
E next = get(i);
lastRet = i;
cursor = i + 1; //1.将索引指向了下一个位置
return next; //2. 返回当前索引的值
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}

public void remove() {
if (lastRet < 0) //lastRet<0表示已经不存在了
throw new IllegalStateException();
checkForComodification();

try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--; //原位置的索引值减小了1,但是实际位置没变
lastRet = -1; //置为-1表示已删除
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}

final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}

private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
cursor = index;
}

public boolean hasPrevious() {
return cursor != 0;
}

public E previous() {
checkForComodification();
try {
int i = cursor - 1; //previous()方法中也做了两件事:
E previous = get(i); //1. 将索引向前移动一位
lastRet = cursor = i; //2. 返回索引处的值
return previous;//总结来说就是返回当前元素并将游标前移一位
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}

public int nextIndex() { //iterator中的index本来就是下一个位置,在next()方法中可以看出
return cursor;
}

public int previousIndex() {
return cursor-1;
}

public void set(E e) { //修改当前位置的元素
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();

try {
AbstractList.this.set(lastRet, e);
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}

public void add(E e) { //在当前位置添加元素
checkForComodification();

try {
int i = cursor;
AbstractList.this.add(i, e);
lastRet = -1;
cursor = i + 1;//添加之后游标改变
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
/**********************************************************************************/

//获得子List,详细源码往下看SubList类
public List<E> subList(int fromIndex, int toIndex) {
return (this instanceof RandomAccess ?
new RandomAccessSubList<>(this, fromIndex, toIndex) :
new SubList<>(this, fromIndex, toIndex));
}

/*************************** Comparison and hashing *******************************/
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof List))
return false;

ListIterator<E> e1 = listIterator();
ListIterator e2 = ((List) o).listIterator();
while (e1.hasNext() && e2.hasNext()) {
E o1 = e1.next();
Object o2 = e2.next();
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
}
return !(e1.hasNext() || e2.hasNext());
}

public int hashCode() { //hashcode
int hashCode = 1;
for (E e : this)
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
return hashCode;
}
/**********************************************************************************/
protected transient int modCount = 0;

private void rangeCheckForAdd(int index) {
if (index < 0 || index > size())
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size();
}
}

class SubList<E> extends AbstractList<E> {
private final AbstractList<E> l;
private final int offset;
private int size;
/* 从SubList源码可以看出,当需要获得一个子List时,底层并不是真正的返回一个子List,还是原来的List,只不过
* 在操作的时候,索引全部限定在用户所需要的子List部分而已。因此,如果你获得了List的一个子串,那么你对子串进行了改变,那么原List也会随之改变的。
*/
SubList(AbstractList<E> list, int fromIndex, int toIndex) {
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
if (toIndex > list.size())
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex + ")");
l = list; //原封不动的将原来的list赋给l
offset = fromIndex; //偏移量,用在操作新的子List中
size = toIndex - fromIndex; //子List的大小,所以子List中不包括toIndex处的值,即子List中包括左边不包括右边
this.modCount = l.modCount;
}
//注意下面所有的操作都在索引上加上偏移量offset,相当于还是在原来的List上操作子List。
//而且,下面的modify操作都会先调用checkForComodification()方法(涉及到线程修改的问题)。
public E set(int index, E element) {
rangeCheck(index);
checkForComodification();
return l.set(index+offset, element);
}

public E get(int index) {
rangeCheck(index);
checkForComodification();
return l.get(index+offset);
}

public int size() {
checkForComodification();
return size;
}

public void add(int index, E element) {
rangeCheckForAdd(index);
checkForComodification();
l.add(index+offset, element);
this.modCount = l.modCount;
size++;
}

public E remove(int index) {
rangeCheck(index);
checkForComodification();
E result = l.remove(index+offset);
this.modCount = l.modCount;
size--;
return result;
}

protected void removeRange(int fromIndex, int toIndex) {
checkForComodification();
l.removeRange(fromIndex+offset, toIndex+offset);
this.modCount = l.modCount;
size -= (toIndex-fromIndex);
}

public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}

public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
int cSize = c.size();
if (cSize==0)
return false;

checkForComodification();
l.addAll(offset+index, c);
this.modCount = l.modCount;
size += cSize;
return true;
}

public Iterator<E> iterator() {
return listIterator();
}

public ListIterator<E> listIterator(final int index) {
checkForComodification();
rangeCheckForAdd(index);

return new ListIterator<E>() {
private final ListIterator<E> i = l.listIterator(index+offset); //相当子List的索引0

public boolean hasNext() {
return nextIndex() < size;
}

public E next() {
if (hasNext())
return i.next();
else
throw new NoSuchElementException();
}

public boolean hasPrevious() {
return previousIndex() >= 0;
}

public E previous() {
if (hasPrevious())
return i.previous();
else
throw new NoSuchElementException();
}

public int nextIndex() {
return i.nextIndex() - offset;
}

public int previousIndex() {
return i.previousIndex() - offset;
}

public void remove() {
i.remove();
SubList.this.modCount = l.modCount;
size--;
}

public void set(E e) {
i.set(e);
}

public void add(E e) {
i.add(e);
SubList.this.modCount = l.modCount;
size++;
}
};
}

public List<E> subList(int fromIndex, int toIndex) {
return new SubList<>(this, fromIndex, toIndex);
}

private void rangeCheck(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

private void rangeCheckForAdd(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}

private void checkForComodification() {
if (this.modCount != l.modCount)
throw new ConcurrentModificationException();
}
}

class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
super(list, fromIndex, toIndex);
}

public List<E> subList(int fromIndex, int toIndex) {
return new RandomAccessSubList<>(this, fromIndex, toIndex);
}
}


AbstractSet接口

AbstractSet接口的实现和AbstractCollection的实现基本一样,而且1.7和1.8并没有改动。与AbstractCollection的实现,重写了Object中的equals()和hashcode()方法。

package java.util;

public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {

protected AbstractSet() {
}

// Comparison and hashing

public boolean equals(Object o) {
if (o == this)
return true;

if (!(o instanceof Set))
return false;
Collection<?> c = (Collection<?>) o;
if (c.size() != size())
return false;
try {
return containsAll(c);
} catch (ClassCastException unused)   {
return false;
} catch (NullPointerException unused) {
return false;
}
}

public int hashCode() {
int h = 0;
Iterator<E> i = iterator();
while (i.hasNext()) {
E obj = i.next();
if (obj != null)
h += obj.hashCode();//Set内各个元素hashcode的拼接
}
return h;
}

public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
boolean modified = false;

if (size() > c.size()) {
for (Iterator<?> i = c.iterator(); i.hasNext(); )
modified |= remove(i.next());
} else {
for (Iterator<?> i = iterator(); i.hasNext(); ) {
if (c.contains(i.next())) {
i.remove();
modified = true;
}
}
}
return modified;
}

}


写的有点多了,这篇就到这里吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: