Java-Collection

interface Collection

集合层次结构中的根接口。集合代表一组对象,称为其元素。一些集合允许重复的元素,而另一些则不允许。有些是有序的,而另一些则是无序的。JDK不提供 此接口的任何直接实现:它提供了更多特定子接口的实现,例如{@code Set}和{@code List}。该接口通常用于传递集合并在需要最大通用性的地方操纵它们。

Bags或multisets(可能包含重复元素的无序集合)应直接实现此接口。

所有通用的{@code Collection}实现类(通常通过其子接口之一间接实现{@code Collection})应提供两个“标准”构造函数:void(无自变量)构造函数,该构造函数创建一个空的集合,以及一个带有单个实参类型为{@code Collection}的构造函数,该构造函数创建一个新集合,其元素与实参相同。实际上,后一个构造函数允许用户复制任何集合,从而产生所需实现类型的等效集合。没有强制执行此约定的方法(因为接口不能包含构造函数),但是Java平台库中的所有通用{@code Collection} 实现都可以遵循。

1
2
3
4
5
6
7
8
9
10
@see     Set
@see List
@see SortedSet
@see HashSet
@see TreeSet
@see ArrayList
@see LinkedList
@see Vector
@see AbstractCollection
// 以上这些实现了Collection接口

方法解析

1.size()返回集合大小

1
2
3
4
5
6
7
8
/**
* Returns the number of elements in this collection. If this collection
* contains more than {@code Integer.MAX_VALUE} elements, returns
* {@code Integer.MAX_VALUE}.
*
* @return the number of elements in this collection
*/
int size();

2.isEmpty()

1
2
3
4
5
6
/**
* Returns {@code true} if this collection contains no elements.
*
* @return {@code true} if this collection contains no elements
*/
boolean isEmpty();

3.contains(Object o)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* Returns {@code true} if this collection contains the specified element.
* More formally, returns {@code true} if and only if this collection
* contains at least one element {@code e} such that
* {@code Objects.equals(o, e)}.
*
* @param o element whose presence in this collection is to be tested
* @return {@code true} if this collection contains the specified
* element
* @throws ClassCastException if the type of the specified element
* is incompatible with this collection
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
* @throws NullPointerException if the specified element is null and this
* collection does not permit null elements
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
*/
boolean contains(Object o);

4.iterator()返回一个迭代器

1
2
3
4
5
6
7
8
9
/**
* Returns an iterator over the elements in this collection. There are no
* guarantees concerning the order in which the elements are returned
* (unless this collection is an instance of some class that provides a
* guarantee).
*
* @return an {@code Iterator} over the elements in this collection
*/
Iterator<E> iterator();

5.toArray()转换成数组

1
2
3
4
5
6
7
8
Object[] toArray();

<T> T[] toArray(T[] a);

default <T> T[] toArray(IntFunction<T[]> generator) {
return toArray(generator.apply(0));
}

6.add()添加元素

1
boolean add(E e);

7.去除元素

1
boolean remove(Object o);

8.containAll()返回是否包含该集合的所有元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* Returns {@code true} if this collection contains all of the elements
* in the specified collection.
*
* @param c collection to be checked for containment in this collection
* @return {@code true} if this collection contains all of the elements
* in the specified collection
* @throws ClassCastException if the types of one or more elements
* in the specified collection are incompatible with this
* collection
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
* @throws NullPointerException if the specified collection contains one
* or more null elements and this collection does not permit null
* elements
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>),
* or if the specified collection is null.
* @see #contains(Object)
*/
boolean containsAll(Collection<?> c);

9.addALL()

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

10.removeAll

1
boolean removeAll(Collection<?> c);

11.removesif()去除所有符合条件的元素

1
2
3
4
5
6
7
8
9
10
11
12
13

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

12.retainAll()去除所有不在c中的元素

1
2
3
4
5
6
7
8
9
10
boolean retainAll(Collection<?> c);

/**
* Removes all of the elements from this collection (optional operation).
* The collection will be empty after this method returns.
*
* @throws UnsupportedOperationException if the {@code clear} operation
* is not supported by this collection
*/

13.clear()清楚所有

1
void clear();

14.equals和hashcode

15.可分割迭代器

1
2
3
4
@Override
default Spliterator<E> spliterator() {
return Spliterators.spliterator(this, 0);
}

16.Stream 获得流

1
2
3
4
//这个是串行流
default Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}

Java 8 API添加了一个新的抽象称为流Stream,可以让你以一种声明的方式处理数据。

Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达的高阶抽象。

Stream API可以极大提高Java程序员的生产力,让程序员写出高效率、干净、简洁的代码。

这种风格将要处理的元素集合看作一种流, 流在管道中传输, 并且可以在管道的节点上进行处理, 比如筛选, 排序,聚合等。

元素流在管道中经过中间操作(intermediate operation)的处理,最后由最终操作(terminal operation)得到前面处理的结果。

1
2
3
+--------------------+       +------+   +------+   +---+   +-------+
| stream of elements +-----> |filter+-> |sorted+-> |map+-> |collect|
+--------------------+ +------+ +------+ +---+ +-------+

以上的流程转换为 Java 代码为:

1
2
3
4
5
6
List<Integer> transactionsIds = 
widgets.stream()
.filter(b -> b.getColor() == RED)
.sorted((x,y) -> x.getWeight() - y.getWeight())
.mapToInt(Widget::getWeight)
.sum();

参考https://www.runoob.com/java/java8-streams.html

17.获得并行流

1
2
3
4
//并行流
default Stream<E> parallelStream() {
return StreamSupport.stream(spliterator(), true);
}