0

This Lambda 8 method came up in one of the suggested answers on leetcode.com: https://leetcode.com/problems/merge-intervals/discuss/21222/A-simple-Java-solution

Below is the method I cannot seem to understand:

int[][] intervals = {{8,10}, {1,3},{2,6},{15,18}};

Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[0], i2[0]));

I understand that the Arrays.sort() sorts the array in ascending order and the second argument is supposed to be the range it sorts the array.

What I don't understand is the i1 and i2 arguments and the Integer.compare() method that follows.

What exactly are i1 and i2 here? Are they arrays or the integers?

How come we don't write (int i1, int i2)? Is this because we have already mentioned Integer afterward?

1
  • 3
    You could write (int[] i1, int[] i2) but it's not necessary because the compiler assumes that the comparator for sorting an int[][] will be Comparator<int[]>. Commented Oct 6, 2020 at 14:24

4 Answers 4

5

I understand that the Arrays.sort() sorts the array in ascending order and the second argument is supposed to be the range it sorts the array.

That is not correct. When you read the javadocs you can see that the second argument is a Comparator - method that defines "rules" on how the array is supposed to be sorted.

int[][] is an array of arrays of ints. If you take a single element of that, it will be plain array of ints - int[]. That is the element you want to be comparing in the sort method and that is what i1 and i2 are. But those are just variable names used inside of the lambda - those could be anything else.

Integer.compare(i1[0], i2[0]) this takes first elements of each array and compares based on that.

How come we don't write (int i1, int i2)? Is this because we have already mentioned Integer afterward?

The method is defined as public static <T> void sort(T[] a, Comparator<? super T> c). When you pass int[][] as the first parameter to it, compilator assumes that second has to be of type Comparator<? super int[]>. Because of that, parameters of the lambda are int[] as well.

Sign up to request clarification or add additional context in comments.

Comments

3

The second argument is the comparator you are using to sort your array. The arrays you are trying to sort is a two-dimension array, so you need to provide a comparator to sort an array of arrays with size two.

With this lambda expression you are comparing two arrays, i1 and i2 with size equal two. Integer.compare(i1[0], i2[0]) is comparing the two arrays of size two based upon their first element.

Comments

2

the sort method is generic method defined like:

public static <T> void sort(T[] a, Comparator<? super T> c)

as you see the type of Comparator c implementation is being inferred during the method's call and, because in your situation, intervals is array of in arrays the expected type of i1 and i2 will be int[]

Comments

1

Arrays.sort takes as a second argument a Comparator<T>.

A Comparator<T> is an interface, and its implementations will take two elements of type <T> and determine which goes first. So, implementing the Comparator interface is useful when you need to sort elements that are not primitive types (like int, char, etc).

In this case the elements that need to be sorted by Arrays.sort are not primitives but other arrays of two elements, so it won't be able to sort them, it needs to know how. So that's why it is providing that Comparator implementation as a second argument.

You see that the lambda is taking i1 and i2 which are arrays, and comparing them by its first element ix[0].

Comments

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.