如何验证 List 的实例不是另一个 List 实例?
How to verify an instance of a List is not another List instance?
我有一个列表
var theDataList: List<Data> // populated with some data
并复制了一份
val copy = theDataList.toMutableList()
代码下游要验证是副本还是原件
.hashCode()
returns 两者相同
如果只想用Log打印出来,怎么办?
Log.d("+++", "theDataList: ${theDataList.hashCode()}, copy: ${copy.hashCode()"})
打印出相同的数字。
并且Log.d("+++", "copy: ${copy}")
打印出列表内容
使用===
运算符比较引用是否相同(不是调用equals
方法)
问题:
两个列表的哈希码相同,因为它基于列表中的数据,是相同的。
解法:
您真正想要的是比较两个列表的引用。您可以使用 Kotlin 的 referential equality operator ===
.
theDataList === copy // false
没有 ID/hash 您可以依靠以您想要的方式识别 JVM 上的对象。有关更多信息,请查看 here.
我有一个列表
var theDataList: List<Data> // populated with some data
并复制了一份
val copy = theDataList.toMutableList()
代码下游要验证是副本还是原件
.hashCode()
returns 两者相同
如果只想用Log打印出来,怎么办?
Log.d("+++", "theDataList: ${theDataList.hashCode()}, copy: ${copy.hashCode()"})
打印出相同的数字。
并且Log.d("+++", "copy: ${copy}")
打印出列表内容
使用===
运算符比较引用是否相同(不是调用equals
方法)
问题:
两个列表的哈希码相同,因为它基于列表中的数据,是相同的。
解法:
您真正想要的是比较两个列表的引用。您可以使用 Kotlin 的 referential equality operator ===
.
theDataList === copy // false
没有 ID/hash 您可以依靠以您想要的方式识别 JVM 上的对象。有关更多信息,请查看 here.