3

I've recently got into functional programming and Java 8 lambdas. I have an array of ints and I want to sort it in an ascending order.

The way I am trying to do this with lambda is as follows:

Arrays.stream(intArray).sorted((x, y) -> Integer.compare(x, y) == -1);

The issue with this is that my compiler says:

Error:(12, 32) java: method sorted in interface 
java.util.stream.IntStream cannot be applied to given types;
required: no arguments
found: (x,y)->Int[...]== -1
reason: actual and formal argument lists differ in length

What am I missing here?

1
  • 4
    Streams do not offer a way to sort an array, they allow to process the elements in a sorted order. The original array is not modified and in your case, since there is no actual operation, nothing will happens at all. To sort an array, use Arrays.sort(array); that’ll work for int[] and Integer[]. Commented Apr 2, 2016 at 8:12

3 Answers 3

5

A Comparator takes two Object and returns an int.

Here, we're entering with two Object's but are returning a boolean expression (Integer.compare(x, y) == -1)

You actually just need the first part of it

Arrays.stream(intArray)
      .sorted((x, y) -> Integer.compare(x, y))
      .toArray(Integer[]::new);

Since you seem to be using an array of Integer objects (because an IntStream doesn't have a sorted(Comparator) method) you better be using a simple IntStream, it will simplify your life because the int will than be sorted in their natural order.

IntStream#sorted()

Returns a stream consisting of the elements of this stream in sorted order.

Arrays.stream(intArray)
      .mapToInt(x->x)
      .sorted()
      .toArray();
Sign up to request clarification or add additional context in comments.

7 Comments

That was my original approach. But even doing it this way, it still gives me the "Integer.Compare(x, y)" cannot be applied to <lambda expression> , <lambda expression>
@Kobek May you please post the full code? With the intArray, it'll help reproduce the problem.
Both of these are incomplete; because there is no terminal operation, neither does anything. You want a toArray() at the end (or similar.)
@BrianGoetz You were totally right. Is it better now?
Since the result is the sorted array returned by toArray() you should assign the result to a variable (in the first case, assigning it to the existing intArray would be fine, in the second, the result is int[] where the source is Integer[])…
|
3

You can do

Arrays.stream(intArray).sorted().forEach(System.out::println);  

The error in your case is because IntStream#sorted() does not take parameters.

DEMO

Comments

1

Arrays.stream(intArray).sorted() Should be used for processing the array in a specific order. It can be used to sort arrays as explained in other answers but it will generate a new sorted array.

Arrays.stream(intArray)
      .sorted((x, y) -> Integer.compare(x, y))
      .toArray(Integer[]::new);

In case of very big sized data this will not be efficient.

If you want to do in-place sorting, you can use Collections.sort()

Arrays.sort(arr, (x,y) -> Integer.compare(x,y))

Or in the case of objects of type A and sorting based on field P

Arrays.sort(arr, Comparator.comparing(A::getP))

where getP() is the method that gets the field P value.

In case of reversed sorting

 Arrays.sort(arr, Comparator.comparing(A::getP).reversed())

In case of sorting on 2 fields P and P2

Arrays.sort(arr, Comparator.comparing(A::getP).thenComparing(A::getP2))

1 Comment

How to sort a raw array like int[] arr with a lambda that has multiple lines (various if elses). Something like this: Arrays.sort(arr, <multi-line lambda>);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.