Collection接口

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

    //return集合中元素数量[如果大于Integer.MAX_VALUE则返回Integer.MAX_VALUE]
    int size();

    //return是否集合中不包含任何元素
    boolean isEmpty();

    //return当前集合是否包含指定元素o
    //throws ClassCastException 指定元素o与集合不兼容
    //throws NullPointerException 当前集合不允许null&&指定元素o为null
    boolean contains(Object o);

    //return当前集合的Iterator
    Iterator<E> iterator();

    //return包含当前集合所有元素的数组
    Object[] toArray();

    //return包含当前集合所有元素的数组[精确控制运行时类型]
    //throws ArrayStoreException 如果T不是每个元素的运行时类型的超类型(父类型)
    <T> T[] toArray(T[] a);

    //return集合是否改变
    //throws UnsupportedOperationException 当前集合不支持add操作
    //throws ClassCastException 指定元素的类E不允许add
    //throws NullPointerException 当前集合不允许null值add
    //throws IllegalArgumentException 不允许元素的某些属性add
    //throws IllegalStateException 在某些时机不允许add
    boolean add(E e);

    //return是否删除某个元素
    //throws ClassCastException 当前集合与元素o不兼容
    //throws NullPointerException 当前集合不允许null值(即o为nul)
    //throws UnsupportedOperationException 当前集合不允许remove
    boolean remove(Object o);

    //return当前集合是否包含c集合中的所有元素
    //throws ClassCastException 当前集合与c集合存在元素不兼容
    //throws NullPointerException 当前集合不允许null值,c中包含null值
    boolean containsAll(Collection<?> c);

    //return集合是否改变
    //throws UnsupportedOperationException 当前集合不支持addAll操作
    //throws ClassCastException 指定元素的类E不允许addAll
    //throws NullPointerException 当前集合不允许null值addAll
    //throws IllegalArgumentException 不允许元素的某些属性addAll
    //throws IllegalStateException 在某些时机不允许addAll
    boolean addAll(Collection<? extends E> c);

    //return集合是否改变
    //throws UnsupportedOperationException 当前集合不支持removeAll操作
    //throws ClassCastException 存在元素不兼容
    //throws NullPointerException 当前集合不允许null值
    boolean removeAll(Collection<?> c);

    //return如果删除某个元素是否会抛出异常[默认遍历迭代器]
    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;
    }

    //return集合是否因此改变
    //只会保留存在集合c中的元素
    //throws UnsupportedOperationException 当前集合不支持retainAll
    //throws ClassCastException 存在元素不兼容
    //throws NullPointerException 当前集合不允许null值
    boolean retainAll(Collection<?> c);

    //删除当前集合所有元素
    //throws UnsupportedOperationException 当前集合不允许clear
    void clear();


    boolean equals(Object o);
    int hashCode();

    //1.8新增
    @Override
    default Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, 0);
    }

    //1.8新增
    default Stream<E> stream() {
        return StreamSupport.stream(spliterator(), false);
    }

    //1.8新增
    default Stream<E> parallelStream() {
        return StreamSupport.stream(spliterator(), true);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值