1
次のコードは、「素数の和」の数に制限を見つけるためにJava 8つのストリームとラムダを使用しています:次のJava 8パイプラインがEclipseで型の不一致を報告するのはなぜですか?
import java.util.stream.Stream;
public class FunctionalPrime {
public static void main(String[] args) {
printSumPrimes(6);
}
/**
* Given n>0, print all pairs i & j s.t. 1<=i<=j<=n and i+j is prime
*
* NOTE: This BREAKS in Eclipse because of some Eclipse bug!
* I had to compile it on the command-line using javac.
*
* @param n
*/
public static void printSumPrimes(int n) {
Stream.iterate(1, i -> i + 1)
.limit(n)
.flatMap(
i -> Stream.iterate(1, j -> j + 1).limit(n)
.filter(j -> i <= j)
.map(j -> new int[] { i, j }))
// .forEach(pair -> System.out.println("" + pair[0] + " " +
// pair[1]));
.filter(pair -> isPrime(pair[0] + pair[1]))
.forEach(
pair -> System.out.println("" + pair[0] + " + "
+ pair[1] + " = " + (pair[0] + pair[1])
+ " is prime"));
}
public static boolean isPrime(int n) {
return Stream.iterate(2, i -> i + 1)
.limit((long) Math.sqrt(n))
.noneMatch(i -> n % i == 0);
}
}
のEclipse 4.4.2(BuildId 20150219から0600)は、次のコンパイル時にエラーを報告します。
- 式の型は配列型でなければなりませんが、それは
をObjectに解決され、まだ同じコードは、コマンドライン上の細かい動作します:
何が起こっているのですか? ...