美文网首页
Collection 接口

Collection 接口

作者: Mcq | 来源:发表于2021-03-21 21:31 被阅读0次

一个Collection(集合)表示一组称为其元素的对象。Collection接口用来传递需要最大通用性的对象集合。例如,按约定,所有集合实现都有一个接收Collection的构造函数,称为转换构造函数,因此可以转换集合的类型。

例如,有一个 Collection<String> c,可能是List或Set,创建一个ArrayList:

List<String> list = new ArrayList<String>(c);
// JDK 7 or later
List<String> list = new ArrayList<>(c);

Collection接口的基本方法

int size()
boolean isEmpty()
boolean contains(Object element),
boolean add(E element)
boolean remove(Object element)
and Iterator<E> iterator()

Collection接口操作整个集合的方法

boolean containsAll(Collection<?> c),
boolean addAll(Collection<? extends E> c)
boolean removeAll(Collection<?> c)
boolean retainAll(Collection<?> c)
and void clear()

Collection接口的数组方法

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

JDK 8 及其之后, Collection接口暴露了 Stream<E> stream() 和 Stream<E> parallelStream()方法,用于从基础集合获取顺序流或并行流。(参考 聚合操作

遍历 Collections

三种方式:聚合操作,for-each,Iterator

聚合操作

JDK 8 及之后,推荐获取流执行聚合的方式遍历集合。

myShapesCollection.stream()
.filter(e -> e.getColor() == Color.RED)
.forEach(e -> System.out.println(e.getName()));

如果集合足够大且电脑核心数足够,可以使用并行流:

myShapesCollection.parallelStream()
.filter(e -> e.getColor() == Color.RED)
.forEach(e -> System.out.println(e.getName()));

多种方式收集数据:

String joined = elements.stream()
    .map(Object::toString)
    .collect(Collectors.joining(", "));

int total = employees.stream()
.collect(Collectors.summingInt(Employee::getSalary)));

for-each

for (Object o : collection)
    System.out.println(o);

Iterators

Iteratos接口,remove方法只能每次调用next后调用一次,否则会报错。
Iterator.remove是在迭代期间,唯一安全的删除元素的方法。

public interface Iterator<E> {
    boolean hasNext();
    E next();
    void remove(); //optional
}

使用Iterator而不是for-eahc的情况:

  1. 删除当前元素;
  2. 并行迭代多个集合。
static void filter(Collection<?> c) {
    for (Iterator<?> it = c.iterator(); it.hasNext(); )
        if (!cond(it.next()))
            it.remove();
}

Collection批量操作

大多情况下这些操作的效率较低。

boolean containsAll(Collection<?> c),
boolean addAll(Collection<? extends E> c)
boolean removeAll(Collection<?> c)
boolean retainAll(Collection<?> c)
and void clear()

例如,从Collection c中删除所有 e:

c.removeAll(Collections.singleton(e));

删除所有null:

c.removeAll(Collections.singleton(null));

Collections.singleton,一个静态方法,返回包含指定元素的不可变Set。

Collection接口Array 操作

toArray作为集合和旧api之间的桥梁,旧api希望在输入时使用数组。
将Collection转为与之长度一致的数组。

无参数时,返回一个Object数组

Object[] a = c.toArray();

如果是Collection<String>

String[] a = c.toArray(new String[0]);

相关文章

  • JAVA中的集合框架 List (二)

    Collection接口List接口简介 Collection接口、子接口及其实现类,Collection接口是j...

  • 2018-07-19 二、Collection及Map接口:

    (一)Collection接口 1.collection接口 Collection接口是Set,Queue,Lis...

  • Java集合笔记

    Collection Collection:Collection接口是集合类的根接口,它有两个接口,List是一个...

  • java集合笔记

    认识Collection接口 Collection接口是单值保存的最大父类接口。Collection中的常用方法如...

  • Collection接口、 List接口

    Collection接口 概述(通过查看API)Collection 层次结构中的根接口。Collection 表...

  • 查看源码——List

    List接口是继承于Collection接口,子接口是AbstractList,Collection/List/A...

  • Collection(集合)

    Collection接口 Collection接口包括List接口和Set接口 1 list接口: 包括三个实现类...

  • 1:java.util包笔记

    1:接口 Collection: Collection 层次结构中的根接口。Collection 表示一组对象,这...

  • 常见的集合有哪些?

    所有集合的顶级接口:Collection和Map。 Collection接口包括:List接口和Set接口。set...

  • 集合

    Collection接口 Collection(单列集合的根接口) List(子接口): |--元素是有序的,元素...

网友评论

      本文标题:Collection 接口

      本文链接:https://www.haomeiwen.com/subject/aryyfltx.html