JAVA8コレクターの結果を印刷しようとしたときに、曖昧エラー

Gunjanシャー:

JAVA8コレクターの結果を印刷しようとしたときに、私はあいまいエラーを取得しています。

私は中にIDの合計の結果を印刷しようとしていますProductオブジェクトが、次のエラーを取得します:

「メソッドのprintln(ダブル)タイプのPrintStreamため曖昧です」

ここで私はコンパイルエラーを取得していますコードの小さな行は次のようになります。

編集した:詳細については、コードスニペットを追加します:

  1. Product.javaドメインクラス。

パッケージ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;
    }

}
  1. 私はコンパイルエラーを取得していますMain.javaクラス:

私はコンパイルエラーを取得していたコードの行は、次のとおりです。

System.out.println(productList.stream().collect(Collectors.summingDouble(x -> x.getId())));

クラスのスナップショット:

ここでは、画像の説明を入力します。

私は別の行(のうちにコレクターを使用する場合、私はすべてのエラーを取得しないprintln方法)。

なぜ我々はそれを使用する場合JAVA 8人のコレクターの正確な戻り値の型を検出することができ、コンパイラins't println()方法?

コマンドプロンプトで別のアプローチの詳細を追加します:

私は、同じJDKのバージョンとプロンプトコマンドを使用してみましたし、プログラムがコンパイルされ、正常に実行されました。だから、ホルガーによって答えが正しいようです。これは、Eclipseのコンパイラで問題です:

ここでは、画像の説明を入力します。

ホルガー:

これは、Eclipseのコンパイラのバグで、ウサギの穴は、コンパイラエラーよりもさらに深くなります。私はにあなたのコードの例を削減しました

public static void main(String[] args)
{
  println(Stream.of(42).collect(Collectors.summingDouble(d -> d)));
}
public static void println(double x) {}
public static void println(char[] x) {}
public static void println(String x) {}
public static void println(Object x) {}

私は保たれprintln、コンパイラの動作に影響を与える方法を。

There are the methods println(Object x), which is the one which should be invoked, as it is the only one applicable without boxing operations, println(double), which is the one mentioned in the error message and applicable after unboxing, and the two methods println(char[] x) and println(String x), which are not applicable at all.

Removing the println(double x) method makes the error go away, which would be understandable, even if the error is not correct, but weirdly, removing the println(Object x) method does not solve the error.

And even worse, removing either of the inapplicable methods, println(char[] x) or println(String x), also removes the error, but generates code invoking the wrong, inapplicable method:

public static void main(String[] args)
{
  println(Stream.of(42).collect(Collectors.summingDouble(d -> d)));
}
public static void println(double x) { System.out.println("println(double)"); }
public static void println(char[] x) { System.out.println("println(char[])"); }
//public static void println(String x) { System.out.println("println(String)"); }
public static void println(Object x) { System.out.println("println(Object)"); }
Exception in thread "main" java.lang.ClassCastException: java.lang.Double cannot be cast to [C
    at Tmp2.main(Unknown Source)
public static void main(String[] args)
{
  println(Stream.of(42).collect(Collectors.summingDouble(d -> d)));
}
public static void println(double x) { System.out.println("println(double)"); }
//public static void println(char[] x) { System.out.println("println(char[])"); }
public static void println(String x) { System.out.println("println(String)"); }
public static void println(Object x) { System.out.println("println(Object)"); }
Exception in thread "main" java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String
    at Tmp2.main(Unknown Source)

私たちは不適切と、この動作を認識するために、正式なJava言語仕様の深さに掘るする必要はありません、と思います。

両方の適用不可能なメソッドを削除し、println(char[] x)そしてprintln(String x)、コンパイラは、正しい方法を選択しますprintln(Object x)にわたりprintln(double x)、それは印象的ではありません。

参考のために、私はバージョンOxygen.3aリリース(4.7.3a)、ビルド20180405から1200でテスト。同様に影響を受ける可能性の高い他のバージョンがあります。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=196750&siteId=1