Collection、Iterable、Iterator
一、概念
Collection是集合类的顶级接口,继承自Iterable
二、源码分析
1、Collection
//继承自Iterable<E>
public interface Collection<E> extends Iterable<E> {
int size();//返回collection中元素的个数
boolean isEmpty();//判断collection中元素是否为空,如果为空,则返回true
boolean contains(Object o);//判断collection中是否包含某个元素,如果包含,则返回true
Iterator<E> iterator();//迭代器
Object[] toArray();//将collection中的元素返回成Object数组
<T> T[] toArray(T[] a);//转换为特定类型数组,且数组为list或者list父级
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);//判断list是否包含某个集合的全部
void clear();//清空集合
boolean equals(Object o);//比较某个元素
int hashCode();//返回hashCode
@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);
}
}
2、Iterable
public interface Iterable<T> {
/**
* Returns an iterator over elements of type {@code T}.
* 返回一个对应泛型的迭代器
* @return an Iterator.
*/
Iterator<T> iterator();
/**
* Performs the given action for each element of the {@code Iterable}
* until all elements have been processed or the action throws an
* exception. Unless otherwise specified by the implementing class,
* actions are performed in the order of iteration (if an iteration order
* is specified). Exceptions thrown by the action are relayed to the
* caller.
*
* @implSpec
* <p>The default implementation behaves as if:
* <pre>{@code
* for (T t : this)
* action.accept(t);
* }</pre>
* 执行每个元素给定的行为,直到所有元素被处理或者处理过程中抛出异常
* @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null
* @since 1.8
*/
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
/**
* Creates a {@link Spliterator} over the elements described by this
* {@code Iterable}.
*
* @implSpec
* The default implementation creates an
* <em><a href="Spliterator.html#binding">early-binding</a></em>
* spliterator from the iterable's {@code Iterator}. The spliterator
* inherits the <em>fail-fast</em> properties of the iterable's iterator.
*
* @implNote
* The default implementation should usually be overridden. The
* spliterator returned by the default implementation has poor splitting
* capabilities, is unsized, and does not report any spliterator
* characteristics. Implementing classes can nearly always provide a
* better implementation.
* 分割迭代器,分段对集合进行迭代,提高程序的并行能力,在多线程情形之下,每个线程处理集合一部分,线程安全
* @return a {@code Spliterator} over the elements described by this
* {@code Iterable}.
* @since 1.8
*/
default Spliterator<T> spliterator() {
return Spliterators.spliteratorUnknownSize(iterator(), 0);
}
}
3、Iterator
public interface Iterator<E> {
/**
* 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.)
* 判断是否有下一个元素
* @return {@code true} if the iteration has more elements
*/
boolean hasNext();
/**
* Returns the next element in the iteration.
* 迭代过程返回下个元素
* @return the next element in the iteration
* @throws NoSuchElementException if the iteration has no more elements
*/
E next();
/**
* Removes from the underlying collection the last element returned
* by this iterator (optional operation). This method can be called
* only once per call to {@link #next}. The behavior of an iterator
* is unspecified if the underlying collection is modified while the
* iteration is in progress in any way other than by calling this
* method.
*
* @implSpec
* The default implementation throws an instance of
* {@link UnsupportedOperationException} and performs no other action.
*
* @throws UnsupportedOperationException if the {@code remove}
* operation is not supported by this iterator
* 默认删除方法
* @throws IllegalStateException if the {@code next} method has not
* yet been called, or the {@code remove} method has already
* been called after the last call to the {@code next}
* method
*/
default void remove() {
throw new UnsupportedOperationException("remove");
}
/**
* Performs the given action for each remaining element until all elements
* have been processed or the action throws an exception. Actions are
* performed in the order of iteration, if that order is specified.
* Exceptions thrown by the action are relayed to the caller.
*
* @implSpec
* <p>The default implementation behaves as if:
* <pre>{@code
* while (hasNext())
* action.accept(next());
* }</pre>
* 执行每个元素给定的行为,直到所有元素被处理或者处理过程中抛出异常
* @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null
* @since 1.8
*/
default void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while (hasNext())
action.accept(next());
}
}


浙公网安备 33010602011771号