简体   繁体   English

使用带有 Java 流的收集和映射时出现“不兼容的类型”错误

[英]"Incompatible types" error when using collect and map with java streams

I have this class:我有这门课:

public class AirbnbListing {
...
private int price
...
public int getPrice() { return price; }

And I'm trying to use a java stream to collect the integer prices from an ArrayList of AirbnbListing objects:我正在尝试使用 java 流从 AirbnbListing 对象的 ArrayList 收集整数价格:

ArrayList<AirbnbListing> properties = dataParser.getNeighbourhoodProperties(neighbourhood, 0, -1, 0, "All");
ArrayList<Integer> prices =
    properties.stream()
        .map(AirbnbListing::getPrice)
        .collect(Collectors.toList());

But I'm getting "Incompatiable types: inferenance variable R has incompatible bounds".但我得到“不兼容的类型:推理变量 R 有不兼容的界限”。 Not sure what the issue is.不确定是什么问题。

Create ArrayList By passing argument like.创建ArrayList通过传递参数之类的。

ArrayList<Integer> prices =
    properties.stream()
        .map(AirbnbListing::getPrice)
        .collect(Collectors.toCollection(ArrayList::new));//<------- this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM