Java Collectors.toList() 返回什么样的 List<E> ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21912314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:23:03  来源:igfitidea点击:

What kind of List<E> does Collectors.toList() return?

javalistlambdajava-8collectors

提问by skiwi

I am reading State of the Lambda: Libraries Edition, and am being surprised by one statement:

我正在阅读Lambda 的状态:图书馆版,并对一项声明感到惊讶:

Under the section Streams, there is the following:

Streams部分下,有以下内容:

List<Shape> blue = shapes.stream()
                         .filter(s -> s.getColor() == BLUE)
                         .collect(Collectors.toList());

The document does not state what shapesactually is, and I do not know if it even matters.

该文件没有说明shapes实际情况,我不知道它是否重要。

What confuses me is the following: What kind of concrete Listdoes this block of code return?

令我困惑的是:List这段代码返回什么样的具体内容?

  • It assigns the variable to a List<Shape>, which is completely fine.
  • stream()nor filter()decide what kind of list to use.
  • Collectors.toList()neither specifies the concrete type of List.
  • 它将变量分配给 a List<Shape>,这完全没问题。
  • stream()也不filter()决定使用什么样的列表。
  • Collectors.toList()两者都没有指定 的具体类型List

So, what concretetype (subclass) of Listis being used here? Are there any guarantees?

那么,这里使用的是什么具体类型(子类)List?有任何保证吗?

采纳答案by Rohit Jain

So, what concrete type (subclass) of List is being used here? Are there any guarantees?

那么,这里使用的是 List 的什么具体类型(子类)?有任何保证吗?

If you look at the documentation of Collectors#toList(), it states that - "There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned". If you want a particular implementation to be returned, you can use Collectors#toCollection(Supplier)instead.

如果您查看 的文档Collectors#toList(),它会指出- “对返回的列表的类型、可变性、可序列化性或线程安全性没有任何保证”。如果您希望返回特定的实现,则可以Collectors#toCollection(Supplier)改用。

Supplier<List<Shape>> supplier = () -> new LinkedList<Shape>();

List<Shape> blue = shapes.stream()
            .filter(s -> s.getColor() == BLUE)
            .collect(Collectors.toCollection(supplier));

And from the lambda, you can return whatever implementation you want of List<Shape>.

从 lambda 中,你可以返回任何你想要的实现List<Shape>

Update:

更新

Or, you can even use method reference:

或者,您甚至可以使用方法引用:

List<Shape> blue = shapes.stream()
            .filter(s -> s.getColor() == BLUE)
            .collect(Collectors.toCollection(LinkedList::new));

回答by Peter Lawrey

It doesn't matter, but the concrete type is non-generic as indeed all types are non-generic at runtime.

没关系,但具体类型是非泛型的,因为实际上所有类型在运行时都是非泛型的。

So, what concrete type (subclass) of List is being used here? Are there any guarantees?

那么,这里使用的是 List 的什么具体类型(子类)?有任何保证吗?

I don't think so, but ArrayList or LinkedList seem likely.

我不这么认为,但 ArrayList 或 LinkedList 似乎很可能。

回答by whyem

Navigating through Netbeans (Ctrl + Click), I landed in this code. It seems to be using an ArrayList as Supplier.

在 Netbeans 中导航(Ctrl + 单击),我进入了这段代码。它似乎使用 ArrayList 作为供应商。

public static <T> Collector<T, ?, List<T>> toList() {
    return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add,
                               (left, right) -> { left.addAll(right); return left; },
                               CH_ID);
}