Why does the following Java 8 pipeline report a type mismatch in Eclipse?

The following code uses Java 8 streams and lambdas to find an unlimited number of “sum primes” :

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) reports the following compile-time error :

  • The type of the expression must be an array type but it resolved to Object

And yet the same code runs fine on the command-line :

Command-Line Compiles and Runs

What’s going on? …

Source: java

Leave a Reply