I have been playing a little with lambda statements in Java 8, and am stuck: I don't know why this isn't working.
I want the lambda statement to return the array where the sum of the first element, multiplied with the second element, is the highest, in this case {2,3}.
Any help is appreciated.
public static void main(String[] args)
{
//Test data
final TreeSet<Integer[]> test = new TreeSet<Integer[]>();
test.add( new Integer[]{1,2} );
test.add( new Integer[]{1,3} );
test.add( new Integer[]{1,4} );
test.add( new Integer[]{2,1} );
test.add( new Integer[]{2,2} );
test.add( new Integer[]{2,3} );
Integer[] test1 = test.stream()
.min((Integer[] l , Integer[] r) -> { return Integer.compare( l[0] * l[1] , r[0] * r[1] ); })
.get(); //Run-time error: java.lang.ClassCastException: [Ljava.lang.Integer; cannot be cast to java.lang.Comparable
Integer[] test2 = test.stream()
.min((Integer[] l , Integer[] r) -> { return Integer.valueOf( l[0] * l[1] ).compareTo( r[0] * r[1] ); })
.get(); //Run-time error: java.lang.ClassCastException: [Ljava.lang.Integer; cannot be cast to java.lang.Comparable
}
Thanks,
-PStiger