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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:40:12  来源:igfitidea点击:

Kotlin object expressions: Comparator example

javacomparatoranonymous-classkotlin

提问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 - xworks? How does Kotlin know what y - xmeans to put ybefore xif y < x?

世界上如何覆盖比较方法的y - x作品?Kotlin 如何知道在if之前y - x放什么意思?yxy < 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.sortdoesn'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.sorty - x 的含义一无所知。它只是尊重任何实现者也需要尊重的 Comparator 接口的定义契约(如果它想要工作)。

It just so happens that y - xis 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 sortfunction and the datatype of xand ycan 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 colorCodeand 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