I am getting Ambiguity error while trying to print result of JAVA8 collectors.
I am trying to print result of summation of IDs in Product
object, but getting the following error :
"The method println(double) is ambiguous for the type PrintStream"
Here is a small line of code where I am getting the compilation error:
Edited : Adding code snippet for more details :
- Product.java domain class.
package com.sample.reproduce.bugs;
public class Product {
private double id;
private String productName;
public double getId() {
return id;
}
public void setId(double id) {
this.id = id;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
}
- Main.java class where I am getting compilation error :
Following is the line of code where I am getting compilation error :
System.out.println(productList.stream().collect(Collectors.summingDouble(x -> x.getId())));
Class Snapshot :
I don't get any error if I will use Collector in a separate line (out of println
method).
Why ins't the compiler able to detect the exact return type of JAVA 8 Collectors if we use it in println()
method?
Adding details of another approach with command prompt :
I tried with command prompt with same JDK version and the program was compiled and executed successfully. So the answer by Holger seems correct. This seems issue only with Eclipse compiler :