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

huangapple go评论77阅读模式
英文:

Having trouble understanding the following Lambda method from Java 8

问题

以下是翻译好的内容:

这个 Lambda 8 方法出现在 leetcode.com 上的一个建议答案中:https://leetcode.com/problems/merge-intervals/discuss/21222/A-simple-Java-solution

以下是我似乎无法理解的方法:

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

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

我理解 Arrays.sort() 方法会将数组按升序排序,第二个参数应该是用于对数组进行排序的范围。

我不明白的是 i1i2 参数,以及随后的 Integer.compare() 方法。

这里的 i1i2 到底是什么?它们是数组还是整数?

为什么我们不写 (int i1, int i2)?是因为我们之后已经提到了 Integer 吗?

英文:

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

得分: 5

我明白Arrays.sort()函数对数组进行升序排序,第二个参数应该是指定对数组进行排序的范围。

这是不正确的。当您阅读javadocs时,您会发现第二个参数是一个比较器(Comparator)- 这是一个定义数组排序“规则”的方法。

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

Integer.compare(i1[0], i2[0])这会取两个数组的第一个元素进行比较。

为什么我们不写(int i1, int i2)?这是因为之后我们已经提到了Integer吗?

该方法定义为public static <T> void sort(T[] a, Comparator<? super T> c)。当您将int[][]作为第一个参数传递时,编译器假定第二个参数必须是类型为Comparator<? super int[]>。因此,lambda表达式的参数也是int[]

英文:

> 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

得分: 3

第二个参数是您用于对数组进行排序的比较器。
您尝试对其进行排序的数组是一个二维数组,因此您需要提供一个比较器来对包含两个元素的数组进行排序。

使用这个lambda表达式,您正在比较两个大小为两个的数组,i1i2Integer.compare(i1[0], i2[0]) 基于它们的第一个元素来比较这两个大小为两个的数组。

英文:

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

得分: 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 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

得分: 1

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

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

在这种情况下,需要由Arrays.sort进行排序的元素不是原始类型,而是另外两个元素的数组,因此它无法直接对它们进行排序,它需要知道如何排序。这就是为什么它将那个Comparator实现作为第二个参数提供的原因。

你可以看到,这个lambda表达式接受i1i2这两个数组,并通过它们的第一个元素ix[0]进行比较。

英文:

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

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

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定