Iterator
Iterator主要遍历Collection集合中的元素
public interface Iterator {
/**
* Returns {@code true} if the iteration has more elements.
* (In other words, returns {@code true} if {@link #next} would
* return an element rather than throwing an exception.)*/
boolean hasNext();
E next();
default void remove()
//lambda
default void forEachRemaining(Consumer super E> action) {
Objects.requireNonNull(action);
while (hasNext())
action.accept(next());
}
Collection
参考:https://blog.csdn.net/javaee_gao/article/details/96372530
//因为Collection中有iterator方法,所以每一个子类集合对象都具备迭代器。
/* The root interface in the collection hierarchy*/
public interface Collection extends Iterable {
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator iterator();
/* Returns an array containing all of the elements
in this collection.*/
Object[] toArray();
/* This method acts as a bridge between array-based
and collection-based APIs. */
T[] toArray(T[] a);
boolean equals(Object o);
List && Set && Queue
三个接口,一个抽象类,再组合继承实现具体子类
public interface List extends Collection
public interface Set extends Collection {
public interface Queue extends Collection
public abstract class AbstractCollection implements Collection
/ * Queues typically, but do not necessarily, order elements in a
* FIFO (first-in-first-out) manner. Among the exceptions are
* priority queues, which order elements according to a supplied
* comparator, or the elements' natural ordering, and LIFO queues (or
* stacks) which order the elements LIFO (last-in-first-out).*/
//以 FIFO(先进先出)的方式排序各个元素。不过优先级队列和 LIFO 队列(或堆栈)例外
List
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator iterator();
Object[] toArray();
T[] toArray(T[] a);
boolean add(E e);
boolean remove(Object o);
boolean containsAll(Collection> c);
boolean addAll(Collection extends E> c);
E get(int index);
E set(int index, E element);
void add(int index, E element);
E remove(int index);
int indexOf(Object o);
Queue
/* A collection designed for holding elements prior to processing.*/
//Inserts the specified element into this queue if it is possible to do
boolean offer(E e);
// Retrieves and removes the head of this queue
E poll();//@return the head of this queue
//* Retrieves, but does not remove, the head of this queue.
E element();// * @throws NoSuchElementException if this queue is empty
/**
* Retrieves, but does not remove, the head of this queue,
* or returns {@code null} if this queue is empty.
*
* @return the head of this queue, or {@code null} if this queue is empty
*/
E peek();
AbstractCollection
/**
* This class provides a skeletal implementation of the {@code Collection}
* interface, to minimize the effort required to implement this interface.
* To implement an unmodifiable collection, the programmer needs only to
* extend this class and provide implementations for the {@code iterator}
* {@code size} methods. (The iterator returned by the {@code iterator}
* method must implement {@code hasNext} and {@code next}.)*/
public abstract class AbstractCollection implements Collection {
/**
* Sole constructor. (For invocation by subclass constructors, typically
* implicit.)
*/
protected AbstractCollection() {
}
public abstract Iterator iterator();
public abstract int size();
AbstractCollection#contains
public boolean contains(Object o) {
Iterator it = iterator();
if (o==null) {
while (it.hasNext())
if (it.next()==null)
return true;
} else {
while (it.hasNext())
if (o.equals(it.next()))
return true;
}
return false;
}
AbstractCollection#toArray
将集合转为一个数组的方法
public Object[] toArray() {
// Estimate size of array; be prepared to see more or fewer elements
Object[] r = new Object[size()];
Iterator it = iterator();
for (int i = 0; i < r.length; i++)
if (! it.hasNext()) // fewer elements than expected
//hasNext为true,不执行return,false才执行return
return Arrays.copyOf(r, i);
r[i] = it.next();
}
return it.hasNext() ? finishToArray(r, it) : r;
}
AbstractList
/**
* This class provides a skeletal implementation of the {@link List}
* interface to minimize the effort required to implement this interface
* backed by a "random access" data store (such as an array).*/
public abstract class AbstractList extends AbstractCollection
implements List {
protected AbstractList() {
}
AbstractList#indexOf
public int indexOf(Object o) {
ListIterator it = listIterator();
if (o==null) {
while (it.hasNext())
if (it.next()==null)
return it.previousIndex();
} else {
while (it.hasNext())
if (o.equals(it.next()))
return it.previousIndex();
}
return -1;
}
AbstractList#subList
public List subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size());
return (this instanceof RandomAccess ?
new RandomAccessSubList<>(this, fromIndex, toIndex) :
new SubList<>(this, fromIndex, toIndex));
}
//AbstractList里面还有几个private类
AbstractQueue
add方法是调用offer方法,remove方法是调用poll方法,element方法是调用peek方法
public abstract class AbstractQueue extends AbstractCollection
implements Queue {
public boolean add(E e) {
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
}
public E remove() {
E x = poll();
if (x != null)
return x;
else
throw new NoSuchElementException();
}
public E element() {
E x = peek();
if (x != null)
return x;
else
throw new NoSuchElementException();
}
AbstractSet
equals方法和hashCode方法
public abstract class AbstractSet extends AbstractCollection
implements Set {
/**
* Sole constructor. (For invocation by subclass constructors, typically
* implicit.)
*/
protected AbstractSet() {
}
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 | NullPointerException unused) {
return false;
}
}
public int hashCode() {
int h = 0;
Iterator i = iterator();
while (i.hasNext()) {
E obj = i.next();
if (obj != null)
h += obj.hashCode();
}
return h;
}