/** * Returns an iterator over the elements contained in this collection. * * @return an iterator over the elements contained in this collection */ publicabstract Iterator<E> iterator();
AbstractList.iterator
public abstract class AbstractListextends AbstractCollectionimplements List
privateclassItrimplementsIterator<E> { /** * Index of element to be returned by subsequent call to next. */ intcursor=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. */ intlastRet= -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. */ intexpectedModCount= modCount;
public E next() { checkForComodification(); try { inti= cursor; Enext= get(i); lastRet = i; cursor = i + 1; return next; } catch (IndexOutOfBoundsException e) { checkForComodification(); thrownewNoSuchElementException(); } }
size()
1
publicabstractintsize();
public abstract class AbstractListextends AbstractCollectionimplements List public class ArrayListextends AbstractListimplements List, RandomAccess, Cloneable, java.io.Serializable
ArrayList.size()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** * The size of the ArrayList (the number of elements it contains). * * @serial */ privateint size; /** * Returns the number of elements in this list. * * @return the number of elements in this list */ publicintsize() { return size; }
boolean isEmpty()
1 2 3
publicbooleanisEmpty() { return size() == 0; }
boolean contains(Object o)
1 2 3 4 5 6 7 8 9 10 11 12 13
publicbooleancontains(Object o) { Iterator<E> it = iterator(); if (o == null) { while (it.hasNext()) if (it.next() == null) returntrue; } else { while (it.hasNext()) if (o.equals(it.next())) returntrue; } returnfalse; }
public Object[] toArray() { // Estimate size of array; be prepared to see more or fewer elements Object[] r = newObject[size()]; Iterator<E> it = iterator(); for (inti=0; i < r.length; i++) { if (!it.hasNext()) // fewer elements than expected 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 intsize= size(); T[] r = a.length >= size ? a : (T[]) java.lang.reflect.Array .newInstance(a.getClass().getComponentType(), size); Iterator<E> it = iterator();
for (inti=0; i < r.length; i++) { if (!it.hasNext()) { // fewer elements than expected if (a == r) { r[i] = null; // null-terminate } elseif (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; }
public E next() { checkForComodification(); inti= cursor; if (i >= size) thrownewNoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) thrownewConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; }
/** * Returns the hash code value for this collection. While the * <tt>Collection</tt> interface adds no stipulations to the general * contract for the <tt>Object.hashCode</tt> method, programmers should * take note that any class that overrides the <tt>Object.equals</tt> * method must also override the <tt>Object.hashCode</tt> method in order * to satisfy the general contract for the <tt>Object.hashCode</tt> method. * In particular, <tt>c1.equals(c2)</tt> implies that * <tt>c1.hashCode()==c2.hashCode()</tt>. * * @return the hash code value for this collection * @see Object#hashCode() * @see Object#equals(Object) */ inthashCode();
default Streamstream()
返回一个不影响数据源的数据序列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * Returns a sequential {@code Stream} with this collection as its source. * * <p>This method should be overridden when the {@link #spliterator()} * method cannot return a spliterator that is {@code IMMUTABLE}, * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()} * for details.) * * @return a sequential {@code Stream} over the elements in this collection * @implSpec The default implementation creates a sequential {@code Stream} from the * collection's {@code Spliterator}. * @since 1.8 */ default Stream<E> stream() { return StreamSupport.stream(spliterator(), false); }