如何在 Java 8 中使用对方付费电话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26905312/
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-11 03:30:01  来源:igfitidea点击:

How to use collect call in Java 8?

javajava-8java-streamcollect

提问by Alexandru Severin

Lets say we have this boring piece of code that we all had to use:

假设我们有一段我们都必须使用的无聊代码:

ArrayList<Long> ids = new ArrayList<Long>();
for (MyObj obj : myList){
    ids.add(obj.getId());
}

After switching to Java 8, my IDE is telling me that I can replace this code with collect call, and it auto-generates:

切换到 Java 8 后,我的 IDE 告诉我可以用 替换此代码collect call,它会自动生成:

ArrayList<Long> ids = myList.stream().map(MyObj::getId).collect(Collectors.toList());

However its giving me this error:

但是它给了我这个错误:

collect(java.util.stream.Collector) in Steam cannot be applied to: (java.util.stream.Collector, capture, java.util.List>)

蒸汽中的 collect(java.util.stream.Collector) 不能应用于: (java.util.stream.Collector, capture, java.util.List>)

I tried casting the parameter but its giving me undefined Aand R, and the IDE isn't giving any more suggestions.

我尝试强制转换参数,但它给了我 undefinedAR,IDE 没有提供更多建议。

I'm curious as how can you use collect callin this scenario, and I couldn't find any information that could guide me properly. Can anyone shed a light?

我很好奇你如何collect call在这种情况下使用,我找不到任何可以正确指导我的信息。任何人都可以照亮吗?

采纳答案by Boris the Spider

The issue is that Collectors.toList, not surprisingly, returns a List<T>. Not an ArrayList.

问题是Collectors.toList,毫不奇怪,返回一个List<T>. 不是ArrayList.

List<Long> ids = remove.stream()
        .map(MyObj::getId)
        .collect(Collectors.toList());

Program to the interface.

程序到interface.

From the documentation:

从文档:

Returns a Collectorthat accumulates the input elements into a new List. There are no guarantees on the type, mutability, serializability, or thread-safety of the Listreturned; if more control over the returned List is required, use toCollection(Supplier).

返回一个Collector将输入元素累积到一个新的 List。还有的类型,可变性,串行化,或的线程安全没有保证List返回; 如果需要对返回的 List 进行更多控制,请使用 toCollection(Supplier).

Emphasis mine - you cannot even assume that the Listreturned is mutable, let alone that it is of a specific class. If you want an ArrayList:

强调我的 - 你甚至不能假设List返回的是可变的,更不用说它是一个特定的类了。如果你想要一个ArrayList

ArrayList<Long> ids = remove.stream()
        .map(MyObj::getId)
        .collect(Collectors.toCollection(ArrayList::new));

Note also, that it is customary to use import staticwith the Java 8 StreamAPI so adding:

另请注意,习惯上import static与 Java 8 StreamAPI一起使用,因此添加:

import static java.util.stream.Collectors.toCollection;

(I hate starred import static, it does nothing but pollute the namespace and add confusion. But selective import static, especially with the Java 8 utility classes, can greatly reduce redundant code)

(我讨厌 starred import static,它只会污染命名空间并增加混乱。但是有选择性import static,尤其是对于 Java 8 实用程序类,可以大大减少冗余代码)

Would result in:

会导致:

ArrayList<Long> ids = remove.stream()
        .map(MyObj::getId)
        .collect(toCollection(ArrayList::new));

回答by azerafati

I use a lot of collector blocks where I create an empty Array and fill it using a loop so I decided I need a utility class of my own not to write the same lines again ad again, here it is:

我使用了很多收集器块,在其中创建了一个空数组并使用循环填充它,所以我决定我需要一个自己的实用程序类,而不是再次编写相同的行广告,这里是:

public class Collections {

    public static <T, O> List<T> collect(Set<O> items, Function<? super O, ? extends T> mapper) {

    return items.stream().map(mapper).collect(Collectors.toCollection(ArrayList::new));
}

}

}

and use it like this

并像这样使用它

List<Product> prods = Collections.collect(basket.getOrderItems(), OrderItem::getProduct);

or like this

或者像这样

List<Long> prods = Collections.collect(basket.getOrderItems(), (item)->item.getProduct().getId());

Though it might look like much more easier to read, it seems streams are a little slower in these kind of scenarios, look here

虽然看起来更容易阅读,但在这些场景中,流似乎有点慢,请看这里