Collection接口的源码翻译和阅读

Collection开头的注释

看了英文注释,总结以下几点:
①这个接口通常用于传递集合 和 在需要最大通用性时操纵它们。
This interface is typically used to pass collections around and manipulate them where maximum generality is desired.

②所有general-purpose集合实现类应该提供两个标准的构造方法,
一个是 a void(no arguement) constructor which creates an empty collection,一个是 a constructor with a single argument of type Collection ,这个构造器很有用:allows the user to copy any collection, producing an equivalent collection of the desired implementation type

③ 剩下的以后再翻译,有点搞不懂。

Collection结构

在这里插入图片描述
Collection继承了Iterable接口,Iterable接口上写着

 Implementing this interface allows an object to be the target of
 the "for-each loop" statement
 大概意思: 实现这个接口可以让一个对象做循环操作 

然后 Iterable接口有一个方法 forEach ,其他的就不展开说了。

add

 boolean add(E e);

通过读jdk里的英文注释,总结了以下几点:
① 返回true:如果这个集合的数据因为这次add()方法的调用而改变。
②返回false:如果这个集合不允许重复数据,而调用这个方法时集合中已经有 the specified element(指定元素)
③支持该方法的集合可以对被添加的元素作一些限制,比如不允许添加null,或者对被添加元素的type限制。然后这些集合类要在文档中写清楚这些限制。
④如果一个集合拒绝添加the specified element ,并且拒绝的原因不是重复数据的原因,则必须抛出一个异常。
⑤ 为什么要像④这样抛出异常呢? 因为这样可以保证集合中一定能包含the specified element 在 call returns 后。 如下是原句:

This preserves the invariant that a collection always contains the specified element after this call returns.

然后注释中还有一些抛出的异常就不写了,自己去jdk里看。

remove

boolean remove(Object o);

没什么好说的,如果执行完后改变了该集合,那么返回true。

containsAll

boolean containsAll(Collection<?> c);

Returns true if this collection contains all of the elements in the specified collection.

很简单一段注释,如果这个集合包含指定集合(specified collection)的元素,则返回true.

addAll

 boolean addAll(Collection<? extends E> c);

当操作进行时,如果指定集合被改变了,那么 The behavior of this operation is undefined(我也不知道什么意思,猜测是“操作失败”)。
同样也是如果该集合因为调用而发生改变,则返回true。

removeAll

boolean removeAll(Collection<?> c);

调用完成后,该集合中不会有和指定集合一样的元素。

removeIf

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;
    }

如果有元素被删除,则返回true(发现没有,和上面一样,也是如果集合发生改变则返回true)

Predicate

Predicate是一个接口,被@FunctionalInterface修饰,所以是一个函数式接口,可以使用lambda表达式。
里面有一些default方法先不管,关键点是test方法

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);

如果return true则将这个元素删除。

第一种写法
collection.removeIf(new Predicate<Student>() {
    @Override
    public boolean test(Student s) {
	    return s.getAge()>=20;//过滤20岁以上的学生
    }
});

第二种写法
collection.removeIf(
	s -> s.getAge() >= 30
);//过滤20岁以上的学生

containsAll

 boolean retainAll(Collection<?> c);

相当于取交集

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值