1

I'm not sure if this small example has syntax errors or if there's an issue with NetBeans 8.1 / Java 8 update 66. The goal here is to use a lambda compare (as opposed to using a custom compare) as shown in one of the answers here:

java: Arrays.sort() with lambda expression

The eventual goal is to sort an array of indices (0 to n-1) according to an array of values, using a lambda compare. With the answer provided by irfani arief to use Integer instead of int, the third code example in this question does this.

I'm using similar syntax, but I'm getting an error on the line that uses the lambda expression. I've tried the variations shown on the linked to question and answers, but nothing has worked yet.

package x;
import java.util.Arrays;
public class x {
    public static void main(String[] args) {
        int[] A = {3, 1, 2, 0};
        Arrays.sort(A, (a, b) -> a-b);  //error on this line
        for(int i = 0; i < 4; i++)
            System.out.println(A[i]);
    }
}

Changing to Integer as answered by irfani arief solved the issue.

package x;
import java.util.Arrays;
public class x {
    public static void main(String[] args) {
        Integer[] A = {3, 1, 2, 0};
        Arrays.sort(A, (a, b) -> a-b);
        for(int i = 0; i < 4; i++)
            System.out.println(A[i]);
    }
}

This was the real goal, to sort an array of indices according to an array of values using a lambda compare, similar to the way this can be done with C++ std::sort using a lambda compare. Only the array of indices needs to be Integer type, the array of values can be primitive type.

package x;
import java.util.Arrays;
public class x {
    public static void main(String[] args) {
        int[] A = {3, 1, 2, 0};
        Integer[] I = {0, 1, 2, 3};
        Arrays.sort(I, (i, j) -> A[i]-A[j]);
        for (Integer i : I) {
            System.out.println(A[i]);
        }
    }
}
Community
  • 1
  • 1
rcgldr
  • 27,407
  • 3
  • 36
  • 61
  • A lambda compare **is** a custom compare. If you use `sort` with a lambda, the type of the lambda expression is `Comparator`. And it fails for the same reason. – ajb Jan 05 '16 at 06:37
  • @ajb - My goal here is to use a lambda in Java, not to sort integers. – rcgldr Jan 05 '16 at 06:39
  • Your lambda syntax is fine. But the compiler has to figure out a functional interface type to go with the lambda. And there isn't one, because the only one that would go with `sort` is `Comparator`, and there's no `Comparator` for `int` or any other primitive type. So because the compiler can't figure out a type for the lambda, it gives you an error. – ajb Jan 05 '16 at 06:44

1 Answers1

0

First of all, to whoever it was that marked this as a duplicate, please read the whole question before you click on that duplicate button. The man is asking about how to do this in lambda (he's possibly learning java 8), he's not asking how to sort an integer.

And here's the answer, lambda operation on java 8 doesn't seem to work with Primitive, since the comparator that gets replaced with it needs to put it inside <>, e.g. Comparator<? super T> where T is the type that gets compared.

So the solution is simple, just change the int[] part to Integer[]

  • The person that marked it as a duplicate **did** read the whole question. The issue is **exactly** the same; if you use a `sort` with a lambda, the lambda expression needs to evaluate to a `Comparator`. And the question that this was marked as a duplicate of, answered exactly that question: how do you sort an array of `int` using a `Comparator`. – ajb Jan 05 '16 at 06:32
  • @ajb yes, i know that the lambda expression will evaluate to a Comparator, and that is the question being asked here, why won't the above function in lambda works? simply because the lambda will translate into a Comparator class with int type parameter which would fail. I don't see the explanation about this in the question that you linked above. – irfani arief Jan 05 '16 at 06:45
  • Although my question was specifically about lambda compare, I'm wondering if a comparator could be used to sort an array of indices according to another array of values as shown in my third example, and if so, how complicated it would be compared to the lambda syntax. – rcgldr Jan 06 '16 at 05:10