java Kotlin 对象表达式:比较器示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33441015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Kotlin object expressions: Comparator example
提问by Rafael Saraiva
This code basically sorts the array in a descending order:
此代码基本上按降序对数组进行排序:
val arrayList = arrayListOf(1, 5, 2)
Collections.sort(arrayList, object : Comparator<Int> {
override fun compare(x : Int, y: Int) = y - x
})
How in the world does overriding the compare method with y - x
works? How does Kotlin know what y - x
means to put y
before x
if y < x
?
世界上如何覆盖比较方法的y - x
作品?Kotlin 如何知道在if之前y - x
放什么意思?y
x
y < x
回答by eski
This actually has nothing to do with Kotlin. It's related to the Java API's Comparator interface, and how Collections.sort uses it.
这实际上与 Kotlin 无关。它与 Java API 的 Comparator 接口以及 Collections.sort 如何使用它有关。
From the documentation:
从文档:
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
比较它的两个参数的顺序。当第一个参数小于、等于或大于第二个参数时,返回一个负整数、零或正整数。
Now let's try this out for the arguments you gave:
现在让我们为您提供的参数尝试一下:
- 1 - 5 = -4 (a negative integer), so 1 is less than 5.
- 5 - 2 = 3 (a positive integer), so 5 is greater than 2.
- etc...
- 1 - 5 = -4(负整数),所以 1 小于 5。
- 5 - 2 = 3(正整数),所以 5 大于 2。
- 等等...
Collections.sort
doesn't know anything about what y - xmeans. It simply respects the defined contract of the Comparator interface that any implementor also needs to respect (if it wants to work).
Collections.sort
对y - x 的含义一无所知。它只是尊重任何实现者也需要尊重的 Comparator 接口的定义契约(如果它想要工作)。
It just so happens that y - x
is an implementation that does respect that contract, because Math.
碰巧这y - x
是一个确实尊重该合同的实现,因为 Math.
回答by Willi Mentzel
Since Comparator is a SAM interface you can write this code more concise using a lambda:
由于 Comparator 是一个 SAM 接口,您可以使用 lambda 更简洁地编写此代码:
Collections.sort(arrayList, {x : Int, y: Int -> y - x})
Or even
甚至
Collections.sort(arrayList) {
x, y -> y - x
}
since the lambda is the last parameter of the sort
function and the datatype of x
and y
can be inferred.
因为 lambda 是sort
函数的最后一个参数,x
并且y
可以推断出和 的数据类型。
Taking two objects and having to define a single integer for them is an abstraction of a sort definition. You basically specify in which order those elements would be layed out if sorted.
获取两个对象并且必须为它们定义一个整数是排序定义的抽象。您基本上可以指定这些元素在排序时的排列顺序。
For sorting integers it might seem like an overkill, but consider having to sort more complex objects for example instances of a class Car
.
对于整数排序,这似乎有点矫枉过正,但考虑必须对更复杂的对象进行排序,例如 class 的实例Car
。
This class has a colorCode
and you want to sort by that:
这个类有一个colorCode
,你想按它排序:
Collections.sort(cars) {
car1, car2 -> car1.colorCode - car2.colorCode
}
That is how you would define an order for those objects in an abstract way.
这就是您以抽象方式为这些对象定义顺序的方式。
回答by Fredy Mederos
In Kotlin you can also sort elements using the kotlin collections extension functions sort, sorted, sortBy .... etc
在 Kotlin 中,您还可以使用 kotlin 集合扩展函数 sort、sorted、sortBy .... 等对元素进行排序
val arrayList = arrayListOf(1, 5, 2).sorted() //1,2,5
or
或者
val arrayList = arrayListOf(1, 5, 2).sortedDescending() //5,2,1