错误:不兼容的类型:推断变量R具有不兼容的范围

时间:2019-08-19 22:18:55

标签: java

我收到以下编译错误:

javac -cp“ /Users/myname/Desktop/Projects/Project/target/jarname.jar” Util.java -Xlint:unchecked

Util.java:51: error: incompatible types: inference variable R has incompatible bounds
            tags = Arrays.stream(tagArray).collect(Collectors.toList());
                                                  ^
    equality constraints: List<String>
    upper bounds: ArrayList<String>,Object
  where R,A,T are type-variables:
    R extends Object declared in method <R,A>collect(Collector<? super T,A,R>)
    A extends Object declared in method <R,A>collect(Collector<? super T,A,R>)
    T extends Object declared in interface Stream
1 error

现在,我的印象是,您应该可以通过这种方式将所有Stream元素放入列表中。

我对Java有点陌生...在这里我不了解什么?

这是导致Util.java中出现问题的函数:

public static GoogleMerchantDetailsDto convertGoogleMerchantDetails(SqlRow row) {
    Array rawTagArray = (Array) row.get("tags");
    ArrayList<String> tags = new ArrayList();
    if (rawTagArray != null) {
        String[] tagArray = Util.toStringArray(rawTagArray);
        tags = Arrays.stream(tagArray).collect(Collectors.toList());
    }
    String distanceFrom = String.format("%,.1f", row.getDouble("distance_from"));
    String latitude = String.format("%,.6f", row.getDouble("latitude"));
    String longitude = String.format("%,.6f", row.getDouble("longitude"));
    GoogleMerchantDetailsDto googleMerchant = GoogleMerchantDetailsDto.builder().withId(row.getString("id"))
            .withName(row.getString("name")).withTags(tags).withDistanceFrom(distanceFrom).withLatitude(latitude)
            .withLongitude(longitude).withIsFavorite(row.getBoolean("is_favorite"))
            .withWaitlist(row.getInteger("waitlist")).withAddress1(row.getString("vicinity")).build();
    return googleMerchant;
}

1 个答案:

答案 0 :(得分:1)

ArrayList<String>更改为List<String>, 像这样:

public static GoogleMerchantDetailsDto convertGoogleMerchantDetails(SqlRow row) {
    Array rawTagArray = (Array) row.get("tags");
    List<String> tags = new ArrayList();
    if (rawTagArray != null) {
        String[] tagArray = Util.toStringArray(rawTagArray);
        tags = Arrays.stream(tagArray).collect(Collectors.toList());
    }
    // ... rest of the method ...
}