Collection接口研究

以下内容基于jdk1.8

接口Collection分析

img

img

该接口实现了接口Iterable

方法:

int size();

返回元素的个数

boolean isEmpty();

返回是否为空

boolean contains(Object o);

返回是否包含某个对象
抛出异常:

  • @throws ClassCastException 参数不兼容
  • @throws NullPointerException 参数是null,并且容器不允许包含null元素

Object[] toArray();

返回所有元素组成的一个数组
数组是重新分配空间并创建的,它的元素不会由该容器维护,原文:

The returned array will be “safe” in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.

T[] toArray(T[] a);

如果给定的数组大小能容纳该容器所有元素,则返回到给定数组中,否则会新分配一个可以容纳所有元素的数组并返回。

List<Integer> list = new ArrayList<Integer>();
Integer[] a = new Integer[2];
list.add(3);
list.add(5);
list.toArray(a); // a = {3,5}

抛出异常:

  • @throws ArrayStoreException 给定的数组运行类型不是该容器运行类型的超类
  • @throws NullPointerException 给定的数组是null

boolean add(E e);

确保该容器包含该指定的元素,如果因为这个操作改变了容器内的元素返回true,如果该容器已经有了该元素且不允许包含重复的元素则返回false

抛出异常:

  • @throws UnsupportedOperationException 该容器不支持这个操作
  • @throws ClassCastException 给定的元素不能添加进容器中
  • @throws NullPointerException 参数是null,并且容器不允许包含null元素
  • @throws IllegalArgumentException 给定的元素的某些属性决定了它不能被添加进该容器中
  • @throws IllegalStateException 由于某些限制该时间不能添加该元素

boolean remove(Object o);

移除某个元素
抛出异常:

  • @throws ClassCastException
  • @throws NullPointerException
  • @throws UnsupportedOperationException

boolean containsAll(Collection c)

如果该容器包含给定的容器中的所有元素,返回`true`
抛出异常:

- @throws ClassCastException
- @throws NullPointerException

## boolean addAll(Collection c)

将给定容器中的所有元素插入到该容器中
抛出异常:

- @throws UnsupportedOperationException
- @throws ClassCastException
- @throws NullPointerException
- @throws IllegalArgumentException
- @throws IllegalStateException

## boolean removeAll(Collection c)

将该容器和给定容器中共有的元素从该容器中删除
抛出异常:

  • @throws UnsupportedOperationException
  • @throws ClassCastException
  • @throws NullPointerException

default boolean removeIf(Predicate filter)

`Predicate`是JAVA8新增的一个函数式接口(指如果填写lambda表达式则默认实现Predicate的test方法),这个方法删除所有符合条件的元素,如果成功返回`true`
**? super E指的是E或E的父类**
默认实现:

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

调用实例:

```java
Integer[] nums = {1,2,3,4,5,6};
ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(nums));
list.removeIf(num->num>3?true:false);//删除list中大于3的元素
//这里的lambda表达式是简化的写法,将lambda表达式完整的写法是list.removeIf((Integer num)->{return num>3?true:false;});
```

抛出异常:

- @throws NullPointerException
- @throws UnsupportedOperationException 这个元素不能被删除

## boolean retainAll(Collection c);

仅保留给定集合中的元素,删除其他元素。

抛出异常:

  • @throws UnsupportedOperationException
  • @throws ClassCastException
  • @throws NullPointerException

void clear();

移除所有元素。

抛出异常:

  • @throws UnsupportedOperationException

default Stream stream();

返回一个序列流。
默认实现:

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

default Stream parallelStream();

返回一个并行流。
默认实现:

default Stream<E> parallelStream() {
    return StreamSupport.stream(spliterator(), true);
}

原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/collection%e6%8e%a5%e5%8f%a3%e7%a0%94%e7%a9%b6/

(0)
彭晨涛彭晨涛管理者
上一篇 2019年11月8日
下一篇 2019年11月14日

相关推荐

发表回复

登录后才能评论