> 文章列表 > Java集合类源码阅读(一)

Java集合类源码阅读(一)

Java集合类源码阅读(一)

文章目录

  • 一. Iterator(迭代器)
    • 1. Iterator源码
    • 2. ListIterator源码
  • 二. Collection
  • 三. List
  • 四. Vector

在阅读源码之前,我们首先需要知道,java集合的一个继承关系图,如下所示


Java集合类源码阅读(一)

然后按照个集合关系图,逐步阅读源码,分析其设计思想。

一. Iterator(迭代器)

1. Iterator源码

public interface Iterator<E> {boolean hasNext();E next();default void remove() {throw new UnsupportedOperationException("remove");}default void forEachRemaining(Consumer<? super E> action) {Objects.requireNonNull(action); //检查指定的对象引用是否不是 null。此方法主要用于在方法和构造函数中执行参数验证while (hasNext())action.accept(next());}
}

hasNext:判断迭代器中是否还有更多的元素(即当前元素的后面是否还有元素),如果有则返回true
next:返回迭代器中的下一个元素
remove:该方法会将上一个调用next()方法时返回的元素从集合中删除。需要注意的是,只有在调用了next()方法后才能调用remove()方法,否则会抛出IllegalStateException异常

2. ListIterator源码

该接口继承至Iterator接口,并对其功能进行了拓展

public interface ListIterator<E> extends Iterator<E> {boolean hasNext();E next();boolean hasPrevious();E previous();int nextIndex();int previousIndex();void remove();void set(E e);void add(E e);
}

hasPrevious:判断当前元素前面是否还有元素
E previous():返回前面一个元素
nextIndex():返回下一个元素在集合中的索引
set:替换由指定元素返回next的previous最后一个元素或用指定元素返回的最后一个元素(可选操作)。仅当上次调用 或 previous之后既未调用 也未remove调用 时add,才能进行此调用next。
add:将指定的元素插入到列表中(可选操作)。该元素紧接在将由 next返回的元素之前(如果有),以及将由 返回的元素 previous(如果有)之后。

二. Collection

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);boolean add(E e);boolean remove(Object o);boolean containsAll(Collection<?> c);boolean addAll(Collection<? extends E> c);boolean removeAll(Collection<?> c);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;}boolean retainAll(Collection<?> c);void clear();boolean equals(Object o);int hashCode();@Overridedefault 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);}
}

int size(): 返回该集合中元素的数量。
boolean isEmpty(): 如果该集合不包含任何元素,则返回 true,否则返回 false。
boolean contains(Object o): 如果该集合包含指定的元素,则返回 true。
Iterator<E> iterator(): 返回一个迭代器,用于遍历该集合中的元素。
Object[] toArray(): 返回一个包含该集合中所有元素的数组
<T> T[] toArray(T[] a): 将该集合中的元素复制到指定类型的数组中,并返回该数组。
boolean add(E e): 将指定元素添加到该集合中。
boolean remove(Object o): 从该集合中删除指定的元素(如果存在)。
boolean containsAll(Collection<?> c): 如果该集合包含指定集合中的所有元素,则返回 true。
boolean addAll(Collection<? extends E> c): 将指定集合中的所有元素添加到该集合中。
boolean removeAll(Collection<?> c): 从该集合中删除指定集合中的所有元素(如果存在)。
default boolean removeIf(Predicate<? super E> filter): 从该集合中删除满足指定条件的所有元素。
boolean retainAll(Collection<?> c): 从该集合中仅保留指定集合中的元素(如果存在)。
void clear(): 删除该集合中的所有元素。
boolean equals(Object o): 如果该集合与指定对象相等,则返回 true。
int hashCode(): 返回该集合的哈希码。
default Spliterator<E> spliterator(): 返回一个 Spliterator,用于遍历该集合中的元素。
default Stream<E> stream(): 返回一个顺序流,用于遍历该集合中的元素。
default Stream<E> parallelStream(): 返回一个并行流,用于遍历该集合中的元素

三. List

List接口源码,继承了Collection接口

public interface List<E> extends Collection<E> {int size();boolean isEmpty();boolean contains(Object o);Iterator<E> iterator();Object[] toArray();<T> T[] toArray(T[] a);boolean add(E e);boolean remove(Object o);boolean containsAll(Collection<?> c);boolean addAll(Collection<? extends E> c);//从指定的索引位置开始添加元素boolean addAll(int index, Collection<? extends E> c);boolean removeAll(Collection<?> c);boolean retainAll(Collection<?> c);default void replaceAll(UnaryOperator<E> operator) {Objects.requireNonNull(operator);final ListIterator<E> li = this.listIterator();while (li.hasNext()) {li.set(operator.apply(li.next()));}}@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);}}void clear();boolean equals(Object o);int hashCode();E get(int index);E set(int index, E element);void add(int index, E element);E remove(int index);int indexOf(Object o);int lastIndexOf(Object o);//这表示List集合的元素是可以重复的ListIterator<E> listIterator();ListIterator<E> listIterator(int index); //返回集合迭代器List<E> subList(int fromIndex, int toIndex);//返回指定索引范围中的元素@Overridedefault Spliterator<E> spliterator() {return Spliterators.spliterator(this, Spliterator.ORDERED);}
}

四. Vector

Vector是一种基本的数据结构,它是一种动态数组,可以在运行时自动扩展和收缩。Vector可以存储任何类型的对象,包括基本数据类型和自定义类的对象。在Java中,Vector的capacity表示它的存储容量,而capacityIncrement表示向量的容量增量。当创建一个Vector对象时,它的capacity是固定的。如果在向量中添加元素时,超过了其当前容量,则会自动扩展其容量。在扩展容量时,如果指定了capacityIncrement,则会按照该增量来增加容量。如果没有指定capacityIncrement,则会将当前容量加倍来扩展容量。例如,如果创建一个初始容量为10的Vector,并且指定容量增量为5,则当添加第11个元素时,Vector的容量将自动扩展到15。如果没有指定容量增量,则容量将增加到20。可以使用Vector类的capacity()方法来获取当前容量,而使用capacityIncrement()方法来获取容量增量。此外,还可以使用ensureCapacity()方法来确保Vector具有足够的容量,以便存储指定数量的元素,而不需要进行容量调整

public class Vector<E>extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{//创建一个Object数组,说明Vector的底层是数组protected Object[] elementData;  //定义元素数量protected int elementCount;//用于处理Vector结合自增,即每次vector元素自增的步伐protected int capacityIncrement;private static final long serialVersionUID = -2767605614048989439L;//构造函数public Vector(int initialCapacity, int capacityIncrement) {super();if (initialCapacity < 0)throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);this.elementData = new Object[initialCapacity];this.capacityIncrement = capacityIncrement;}//构造函数public Vector(int initialCapacity) {this(initialCapacity, 0);}//默认构造函数public Vector() {this(10);}//用另一个集合来构造Vector集合public Vector(Collection<? extends E> c) {Object[] a = c.toArray();  //将集合c转换为数组形式elementCount = a.length; if (c.getClass() == ArrayList.class) {//c的类型和Arraylist则直接赋值elementData = a;} else {//否则直接拷贝使用elementData = Arrays.copyOf(a, elementCount, Object[].class);}}//将数组从指定的源阵列(从指定位置开始)复制到目标阵列的指定位置,可以看出是线程安全的public synchronized void copyInto(Object[] anArray) {System.arraycopy(elementData, 0, anArray, 0, elementCount);}//trimToSize()方法,它用于将Vector对象的容量调整为当前Vector中实际元素的数量。换句话说,它可以将Vector的容量缩小到与其大小相同,从而节省内存空间public synchronized void trimToSize() {modCount++;int oldCapacity = elementData.length;if (elementCount < oldCapacity) {elementData = Arrays.copyOf(elementData, elementCount);}}public synchronized void ensureCapacity(int minCapacity) {if (minCapacity > 0) {modCount++;ensureCapacityHelper(minCapacity);}}private void ensureCapacityHelper(int minCapacity) {// overflow-conscious codeif (minCapacity - elementData.length > 0)grow(minCapacity);}private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;//该函数用于设置Vector的容量(这个方法不是线程安全的)private void grow(int minCapacity) {//获取旧的vector的容量int oldCapacity = elementData.length;//容量增加,如果capacityIncrement大于0则增加capacityIncrement,否则就是原来容量扩容int newCapacity = oldCapacity + ((capacityIncrement > 0) ?capacityIncrement : oldCapacity);//如果新的容量小于指定的最小容量,则将新容量扩容成指定的最小容量if (newCapacity - minCapacity < 0)newCapacity = minCapacity;//如果新的容量大于了MAX_ARRAY_SIZE,调用hugeCapacity函数(下面有)if (newCapacity - MAX_ARRAY_SIZE > 0)newCapacity = hugeCapacity(minCapacity);//重写设置集合elementData = Arrays.copyOf(elementData, newCapacity);}private static int hugeCapacity(int minCapacity) {if (minCapacity < 0) // overflowthrow new OutOfMemoryError();return (minCapacity > MAX_ARRAY_SIZE) ?Integer.MAX_VALUE :MAX_ARRAY_SIZE;}public synchronized void setSize(int newSize) {modCount++;if (newSize > elementCount) {ensureCapacityHelper(newSize);} else {//指定的新的容量小于本来的容量,则将newSize后面的内容设置为空for (int i = newSize ; i < elementCount ; i++) {elementData[i] = null;}}elementCount = newSize;}//返回当前容器的实际容量public synchronized int capacity() {return elementData.length;}public synchronized int size() {return elementCount;}public synchronized boolean isEmpty() {return elementCount == 0;}//用枚举类封装集合的一些信息public Enumeration<E> elements() {return new Enumeration<E>() {int count = 0;public boolean hasMoreElements() {return count < elementCount;}public E nextElement() {synchronized (Vector.this) {if (count < elementCount) {return elementData(count++);}}throw new NoSuchElementException("Vector Enumeration");}};}//判断vector集合中是否包含某个元素,本质上是用indexof判断的public boolean contains(Object o) {return indexOf(o, 0) >= 0;}//返回指定元素在数组的索引public int indexOf(Object o) {return indexOf(o, 0);}//实现返回指定元素索引的具体方法实现(它是线程安全的),根据实现可以看出它是返回第一个匹配的元素public synchronized int indexOf(Object o, int index) {if (o == null) {for (int i = index ; i < elementCount ; i++)if (elementData[i]==null)return i;} else {for (int i = index ; i < elementCount ; i++)if (o.equals(elementData[i]))return i;}return -1;}public synchronized int lastIndexOf(Object o) {return lastIndexOf(o, elementCount-1);}//它其实是一个逆向遍历的过程public synchronized int lastIndexOf(Object o, int index) {if (index >= elementCount)//如果索引大于实际元素数量,抛出异常throw new IndexOutOfBoundsException(index + " >= "+ elementCount);if (o == null) {for (int i = index; i >= 0; i--)if (elementData[i]==null)return i;} else {for (int i = index; i >= 0; i--)if (o.equals(elementData[i]))return i;}return -1;}//返回指定元素在集合中的索引public synchronized E elementAt(int index) {if (index >= elementCount) {throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);}return elementData(index);}//返回vector的第一个元素public synchronized E firstElement() {if (elementCount == 0) {throw new NoSuchElementException();}return elementData(0);}//返回vector的最后一个元素public synchronized E lastElement() {if (elementCount == 0) {throw new NoSuchElementException();}return elementData(elementCount - 1);}//替换指定索引位置的元素public synchronized void setElementAt(E obj, int index) {if (index >= elementCount) {throw new ArrayIndexOutOfBoundsException(index + " >= " +elementCount);}elementData[index] = obj;}public synchronized void removeElementAt(int index) {modCount++;if (index >= elementCount) {throw new ArrayIndexOutOfBoundsException(index + " >= " +elementCount);}else if (index < 0) {throw new ArrayIndexOutOfBoundsException(index);}int j = elementCount - index - 1;if (j > 0) {//将原数组的index+1位置后面的元素拷贝到原数组的index开始的位置,元素个数为j,即要删除原属后面的元素System.arraycopy(elementData, index + 1, elementData, index, j);}elementCount--;elementData[elementCount] = null; /* to let gc do its work */}//在指定的索引位置插入元素public synchronized void insertElementAt(E obj, int index) {modCount++;if (index > elementCount) {throw new ArrayIndexOutOfBoundsException(index+ " > " + elementCount);}//容量添加ensureCapacityHelper(elementCount + 1);System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);elementData[index] = obj;elementCount++;}//集合末尾添加元素public synchronized void addElement(E obj) {modCount++;ensureCapacityHelper(elementCount + 1);elementData[elementCount++] = obj;}//删除集合中与指定obj匹配的第一个元素public synchronized boolean removeElement(Object obj) {modCount++;int i = indexOf(obj);if (i >= 0) {removeElementAt(i);return true;}return false;}//删除所有元素,(将所有的元素设置为null)public synchronized void removeAllElements() {modCount++;// Let gc do its workfor (int i = 0; i < elementCount; i++)elementData[i] = null;elementCount = 0;}public synchronized Object clone() {try {//@SuppressWarnings("unchecked") 是 Java 中的一个注解(Annotation),它用于告诉编译器忽略 unchecked 警告。当编译器发现代码中存在未检查的转换时(如将一个泛型类型的对象赋给一个非泛型类型的变量,或者将一个带有泛型类型参数的方法返回值赋给一个未使用泛型类型参数的变量),它会生成一个 unchecked 警告@SuppressWarnings("unchecked")Vector<E> v = (Vector<E>) super.clone();v.elementData = Arrays.copyOf(elementData, elementCount);v.modCount = 0;return v;} catch (CloneNotSupportedException e) {// this shouldn't happen, since we are Cloneablethrow new InternalError(e);}}//vector对象转换为数组public synchronized Object[] toArray() {return Arrays.copyOf(elementData, elementCount);}@SuppressWarnings("unchecked")public synchronized <T> T[] toArray(T[] a) {if (a.length < elementCount)return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());System.arraycopy(elementData, 0, a, 0, elementCount);if (a.length > elementCount)a[elementCount] = null;return a;}//查找指定索引位置的元素,现场不安全的@SuppressWarnings("unchecked")E elementData(int index) {return (E) elementData[index];}//同意是返回指定索引位置的元素,但它是线程安全的public synchronized E get(int index) {if (index >= elementCount)throw new ArrayIndexOutOfBoundsException(index);return elementData(index);}//设置集合指定位置的元素值public synchronized E set(int index, E element) {if (index >= elementCount)throw new ArrayIndexOutOfBoundsException(index);E oldValue = elementData(index);elementData[index] = element;return oldValue;}//向vector集合的末尾添加元素public synchronized boolean add(E e) {modCount++;ensureCapacityHelper(elementCount + 1);elementData[elementCount++] = e;return true;}public boolean remove(Object o) {return removeElement(o);}public void add(int index, E element) {insertElementAt(element, index);}public synchronized E remove(int index) {modCount++;if (index >= elementCount)throw new ArrayIndexOutOfBoundsException(index);E oldValue = elementData(index);int numMoved = elementCount - index - 1;if (numMoved > 0)System.arraycopy(elementData, index+1, elementData, index,numMoved);elementData[--elementCount] = null; // Let gc do its workreturn oldValue;}public void clear() {removeAllElements();}public synchronized boolean containsAll(Collection<?> c) {return super.containsAll(c);}//将某个Conlleciton中的所有元素添加到当前vector集合中public synchronized boolean addAll(Collection<? extends E> c) {modCount++;Object[] a = c.toArray();int numNew = a.length;ensureCapacityHelper(elementCount + numNew);System.arraycopy(a, 0, elementData, elementCount, numNew);elementCount += numNew;return numNew != 0;}//删除集合所有的元素public synchronized boolean removeAll(Collection<?> c) {return super.removeAll(c);}//在集合中保留指定集合的元素public synchronized boolean retainAll(Collection<?> c) {return super.retainAll(c);}//将Collection的元素从指定位置加入到集合中public synchronized boolean addAll(int index, Collection<? extends E> c) {modCount++;if (index < 0 || index > elementCount)throw new ArrayIndexOutOfBoundsException(index);Object[] a = c.toArray();int numNew = a.length;ensureCapacityHelper(elementCount + numNew);int numMoved = elementCount - index;if (numMoved > 0)System.arraycopy(elementData, index, elementData, index + numNew,numMoved);System.arraycopy(a, 0, elementData, index, numNew);elementCount += numNew;return numNew != 0;}public synchronized boolean equals(Object o) {return super.equals(o);}public synchronized int hashCode() {return super.hashCode();}public synchronized String toString() {return super.toString();}//返回指定位置区间的原属内容public synchronized List<E> subList(int fromIndex, int toIndex) {return Collections.synchronizedList(super.subList(fromIndex, toIndex),this);}//删除指定区间的元素protected synchronized void removeRange(int fromIndex, int toIndex) {modCount++;int numMoved = elementCount - toIndex;System.arraycopy(elementData, toIndex, elementData, fromIndex,numMoved);// Let gc do its workint newElementCount = elementCount - (toIndex-fromIndex);while (elementCount != newElementCount)elementData[--elementCount] = null;}//用于反序列化private void readObject(ObjectInputStream in)throws IOException, ClassNotFoundException {ObjectInputStream.GetField gfields = in.readFields();int count = gfields.get("elementCount", 0);Object[] data = (Object[])gfields.get("elementData", null);if (count < 0 || data == null || count > data.length) {throw new StreamCorruptedException("Inconsistent vector internals");}elementCount = count;elementData = data.clone();}//用于序列化对象,是线程安全的private void writeObject(java.io.ObjectOutputStream s)throws java.io.IOException {final java.io.ObjectOutputStream.PutField fields = s.putFields();final Object[] data;synchronized (this) {fields.put("capacityIncrement", capacityIncrement);fields.put("elementCount", elementCount);data = elementData.clone();}fields.put("elementData", data);s.writeFields();}//线程安全的迭代器类public synchronized ListIterator<E> listIterator(int index) {if (index < 0 || index > elementCount)throw new IndexOutOfBoundsException("Index: "+index);return new ListItr(index);}public synchronized ListIterator<E> listIterator() {return new ListItr(0);}public synchronized Iterator<E> iterator() {return new Itr();}private class Itr implements Iterator<E> {int cursor;       // index of next element to returnint lastRet = -1; // index of last element returned; -1 if no suchint expectedModCount = modCount;public boolean hasNext() {// Racy but within spec, since modifications are checked// within or after synchronization in next/previousreturn cursor != elementCount;}public E next() {synchronized (Vector.this) {checkForComodification();int i = cursor;if (i >= elementCount)throw new NoSuchElementException();cursor = i + 1;return elementData(lastRet = i);}}public void remove() {if (lastRet == -1)throw new IllegalStateException();synchronized (Vector.this) {checkForComodification();Vector.this.remove(lastRet);expectedModCount = modCount;}cursor = lastRet;lastRet = -1;}//迭代器对象final class ListItr extends Itr implements ListIterator<E> {ListItr(int index) {super();cursor = index;}public boolean hasPrevious() {return cursor != 0;}public int nextIndex() {return cursor;}public int previousIndex() {return cursor - 1;}public E previous() {synchronized (Vector.this) {checkForComodification();int i = cursor - 1;if (i < 0)throw new NoSuchElementException();cursor = i;return elementData(lastRet = i);}}public void set(E e) {if (lastRet == -1)throw new IllegalStateException();synchronized (Vector.this) {checkForComodification();Vector.this.set(lastRet, e);}}public void add(E e) {int i = cursor;synchronized (Vector.this) {//对当前vector对象加上对象锁checkForComodification();Vector.this.add(i, e);expectedModCount = modCount;}cursor = i + 1;lastRet = -1;}}//集合排序是线程安全的@SuppressWarnings("unchecked")@Overridepublic synchronized void sort(Comparator<? super E> c) {final int expectedModCount = modCount;Arrays.sort((E[]) elementData, 0, elementCount, c);if (modCount != expectedModCount) {throw new ConcurrentModificationException();}modCount++;}}