gpt4 book ai didi

kotlin - 如何从 Kotlin 的列表中删除具有 distinctBy 的重复对象?

转载 作者:IT老高 更新时间:2023-10-28 13:32:03 25 4
gpt4 key购买 nike

如何在自定义对象列表上使用 distinctBy 来去除重复项?我想通过对象的多个属性来确定“唯一性”,但不是全部。

我希望这样的事情能奏效,但没有运气:

val uniqueObjects = myObjectList.distinctBy { it.myField, it.myOtherField }

编辑:我很好奇如何将 distinctBy 与任意数量的属性一起使用,而不仅仅是我上面示例中的两个。

最佳答案

您可以创建一对:

myObjectList.distinctBy { Pair(it.myField, it.myOtherField) }

distinctBy 将使用 Pair 的相等性来确定唯一性。

关于kotlin - 如何从 Kotlin 的列表中删除具有 distinctBy 的重复对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45883719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com