# Collection接口分析

image-20251223180626125

# 定义接口

public interface Collection<E> extends Iterable<E> {

}
1
2
3

# 接口定义的抽象操作

获取大小size()方法,返回一个int类型描述大小。

int size();
1

是否为空。true标识集合是空的。false表示集合不是空的。

boolean isEmpty();
1

contains表示:v.包含。集合中是否包含某一个元素、指定集合里面的元素。

参数Object:表示任意对象。

boolean contains(Object o);
1
Iterator<E> iterator();
1
Object[] toArray();
1
<T> T[] toArray(T[] a);
1
boolean add(E e);
1
boolean remove(Object o);
1
boolean containsAll(Collection<?> c);
1
boolean addAll(Collection<? extends E> c);
1
boolean removeAll(Collection<?> c);
1
    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;
    }
1
2
3
4
5
6
7
8
9
10
11
12
    boolean retainAll(Collection<?> c);
1
void clear();
1
boolean equals(Object o);
1
int hashCode();
1
    @Override
    default Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, 0);
    }
1
2
3
4
    default Stream<E> stream() {
        return StreamSupport.stream(spliterator(), false);
    }

1
2
3
4
default Stream<E> parallelStream() {
    return StreamSupport.stream(spliterator(), true);
}
1
2
3
Last Updated: 4/3/2026, 6:47:37 AM