遇到了理解Java 8中以下Lambda方法的困难。

go评论68阅读模式

Having trouble understanding the following Lambda method from Java 8

问题

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

Arrays.sort(intervals, (i1, i2) -&gt; Integer.compare(i1[0], i2[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) -&gt; 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

`int[][]`是一个int数组的数组。如果您取其中的一个单独元素，它将是一个普通的int数组 - `int[]`。这就是您希望在排序方法中进行比较的元素，也是`i1``i2`所代表的。但这些只是lambda表达式内部使用的变量名 - 它们可以是任何其他名称。

`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.

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 &lt;T&gt; void sort(T[] a, Comparator&lt;? super T&gt; c)`. When you pass `int[][]` as the first parameter to it, compilator assumes that second has to be of type `Comparator&lt;? super int[]&gt;`. Because of that, parameters of the lambda are `int[]` as well.

答案2

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.

答案3

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 an array of arrays, the expected type of `i1` and `i2` will be `int[]`.

the `sort` method is generic method defined like:

``````public static &lt;T&gt; void sort(T[] a, Comparator&lt;? super T&gt; 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[]`

答案4

`Arrays.sort`接受第二个参数作为`Comparator<T>`

`Comparator<T>`是一个接口，它的实现将会接受两个类型为`<T>`的元素，并确定哪个应该排在前面。所以，在需要对非原始类型（如`int``char`等）的元素进行排序时，实现`Comparator`接口是很有用的。

`Arrays.sort` takes as a second argument a `Comparator&lt;T&gt;`.

A `Comparator&lt;T&gt;` is an interface, and its implementations will take two elements of type `&lt;T&gt;` 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]`.

• 本文由 发表于 2020年10月6日 22:22:07
• 转载请务必保留本文链接：https://go.coder-hub.com/64227804.html
• java
• lambda

go 67

go 61

go 64

go 49