在 Java 中,Collection 作为集合的顶层设计,一共抽象出了17个公共方法:
public interface Collection<E> extends Iterable<E> {
int size();
boolean isEmpty();
boolean contains(@RecentlyNullable Object var1);
@RecentlyNonNull
Iterator<E> iterator();
@RecentlyNonNull
Object[] toArray();
@RecentlyNonNull
<T> T[] toArray(@RecentlyNonNull T[] var1);
@RecentlyNonNull
default <T> T[] toArray(@RecentlyNonNull IntFunction<T[]> generator) {
throw new RuntimeException("Stub!");
}
boolean add(E var1);
boolean remove(@RecentlyNullable Object var1);
boolean containsAll(@RecentlyNonNull Collection<?> var1);
boolean addAll(@RecentlyNonNull Collection<? extends E> var1);
boolean removeAll(@RecentlyNonNull Collection<?> var1);
default boolean removeIf(@RecentlyNonNull Predicate<? super E> filter) {
throw new RuntimeException("Stub!");
}
boolean retainAll(@RecentlyNonNull Collection<?> var1);
void clear();
boolean equals(@RecentlyNullable Object var1);
int hashCode();
@RecentlyNonNull
default Spliterator<E> spliterator() {
throw new RuntimeException("Stub!");
}
@RecentlyNonNull
default Stream<E> stream() {
throw new RuntimeException("Stub!");
}
@RecentlyNonNull
default Stream<E> parallelStream() {
throw new RuntimeException("Stub!");
}
}
为了防止读起来特别痛苦,我们把以上方法做一些分类,大约可分为三类:修改操作、查询操作、批量操作