为什么 hashMap.toSortedMap returns 少了一个条目
why hashMap.toSortedMap returns one entry less
有一个包含四个条目的 hasMap,在 toSortedMap 之后结果映射只有三个条目。
var uuidToConfigMap = HashMap<UUID, Config>()
配置类型:
data class Config (
val test: String,
val type: Int,
val priority: Int
) {
override fun toString() : String {
return "type:$type, priority:$priority"+", test:"+test
}
}
数据和排序代码,uuidToConfigMap有四个条目:
var uuidToConfigMap = HashMap<UUID, Config>()
uuidToConfigMap[UUID.randomUUID()
] = Config(“xxx”, 1000, 1)
uuidToConfigMap[UUID.randomUUID()
] = Config(“yyy”, 1000, 1)
uuidToConfigMap[UUID.randomUUID()
] = Config(“video”, 100, 2)
uuidToConfigMap[UUID.randomUUID()
] = Config(“news”, 200, 3)
///
for ((_, config) in uuidToConfigMapp) {
Log.d("+++", "+++ config: ${config}”)
}
Log.e("+++", "+++ uuidToConfigMap.size ${uuidToConfigMap.size}")
val sortedUuidToConfigMap = uuidToConfigMap.toSortedMap<UUID, Config>(object: Comparator<UUID>{
override fun compare(o1: UUID?, o2: UUID?): Int {
val config1 = uuidToConfigMap[o1]
val config2 = uuidToConfigMap[o2]
Log.i("+++", "+++ $o1, $o2")
if (config1 == null || config2 == null) {
return -1
}
Log.d("+++", "+++ config: ${config1} <<<>>> ${config2}...")
// sorted map iteration order will be in ascending order
return (config1.priority - config2.priority)
}
})
Log.e("+++", "+++ sortedUuidToConfigMap.size ${sortedUuidToConfigMap.size}")
for ((_, config) in sortedUuidToConfigMa) {
Log.d("+++", "+++ config: ${config}")
}
///
打印输出显示结果 sortedUuidToConfigMa 只有三个条目(条目
config: type:1000, priority:1, test:yyy
缺失):
+++ config: type:200, priority:3, test:video
+++ config: type:100, priority:2, test:news
+++ config: type:1000, priority:1, test:yyy
+++ config: type:1000, priority:1, test:xxx
+++ uuidToConfigMap.size 4
+++ 657f1d4f-4f53-4f1f-83e4-3c624d12751f, 3a1a91da-5921-47f7-9104-c0efa48b6069
+++ config: type:100, priority:2, test:news <<<>>> type:200, priority:3, test:video...
+++ 600380fb-46a3-4cdd-9ce2-2806d6900420, 3a1a91da-5921-47f7-9104-c0efa48b6069
+++ config: type:1000, priority:1, test:yyy <<<>>> type:200, priority:3, test:video...
+++ 600380fb-46a3-4cdd-9ce2-2806d6900420, 657f1d4f-4f53-4f1f-83e4-3c624d12751f
+++ config: type:1000, priority:1, test:yyy <<<>>> type:100, priority:2, test:news...
+++ 77453616-ffff-4dd5-b525-8d5aebc89e92, 657f1d4f-4f53-4f1f-83e4-3c624d12751f
+++ config: type:1000, priority:1, test:xxx <<<>>> type:100, priority:2, test:news...
+++ 77453616-ffff-4dd5-b525-8d5aebc89e92, 600380fb-46a3-4cdd-9ce2-2806d6900420
+++ config: type:1000, priority:1, test:xxx <<<>>> type:1000, priority:1, test:yyy…
+++ sortedUuidToConfigMap.size 3
+++ config: type:1000, priority:1, test:xxx
+++ config: type:100, priority:2, test:news
+++ config: type:200, priority:3, test:video
根据您的比较器,当两个 UUID
存储的优先级相等时,它们是相等的。因此 77453616-ffff-4dd5-b525-8d5aebc89e92
和 600380fb-46a3-4cdd-9ce2-2806d6900420
被认为是相等的,并且只有其中一个最终出现在结果映射中。
有一个包含四个条目的 hasMap,在 toSortedMap 之后结果映射只有三个条目。
var uuidToConfigMap = HashMap<UUID, Config>()
配置类型:
data class Config (
val test: String,
val type: Int,
val priority: Int
) {
override fun toString() : String {
return "type:$type, priority:$priority"+", test:"+test
}
}
数据和排序代码,uuidToConfigMap有四个条目:
var uuidToConfigMap = HashMap<UUID, Config>()
uuidToConfigMap[UUID.randomUUID()
] = Config(“xxx”, 1000, 1)
uuidToConfigMap[UUID.randomUUID()
] = Config(“yyy”, 1000, 1)
uuidToConfigMap[UUID.randomUUID()
] = Config(“video”, 100, 2)
uuidToConfigMap[UUID.randomUUID()
] = Config(“news”, 200, 3)
///
for ((_, config) in uuidToConfigMapp) {
Log.d("+++", "+++ config: ${config}”)
}
Log.e("+++", "+++ uuidToConfigMap.size ${uuidToConfigMap.size}")
val sortedUuidToConfigMap = uuidToConfigMap.toSortedMap<UUID, Config>(object: Comparator<UUID>{
override fun compare(o1: UUID?, o2: UUID?): Int {
val config1 = uuidToConfigMap[o1]
val config2 = uuidToConfigMap[o2]
Log.i("+++", "+++ $o1, $o2")
if (config1 == null || config2 == null) {
return -1
}
Log.d("+++", "+++ config: ${config1} <<<>>> ${config2}...")
// sorted map iteration order will be in ascending order
return (config1.priority - config2.priority)
}
})
Log.e("+++", "+++ sortedUuidToConfigMap.size ${sortedUuidToConfigMap.size}")
for ((_, config) in sortedUuidToConfigMa) {
Log.d("+++", "+++ config: ${config}")
}
///
打印输出显示结果 sortedUuidToConfigMa 只有三个条目(条目
config: type:1000, priority:1, test:yyy
缺失):
+++ config: type:200, priority:3, test:video
+++ config: type:100, priority:2, test:news
+++ config: type:1000, priority:1, test:yyy
+++ config: type:1000, priority:1, test:xxx
+++ uuidToConfigMap.size 4
+++ 657f1d4f-4f53-4f1f-83e4-3c624d12751f, 3a1a91da-5921-47f7-9104-c0efa48b6069
+++ config: type:100, priority:2, test:news <<<>>> type:200, priority:3, test:video...
+++ 600380fb-46a3-4cdd-9ce2-2806d6900420, 3a1a91da-5921-47f7-9104-c0efa48b6069
+++ config: type:1000, priority:1, test:yyy <<<>>> type:200, priority:3, test:video...
+++ 600380fb-46a3-4cdd-9ce2-2806d6900420, 657f1d4f-4f53-4f1f-83e4-3c624d12751f
+++ config: type:1000, priority:1, test:yyy <<<>>> type:100, priority:2, test:news...
+++ 77453616-ffff-4dd5-b525-8d5aebc89e92, 657f1d4f-4f53-4f1f-83e4-3c624d12751f
+++ config: type:1000, priority:1, test:xxx <<<>>> type:100, priority:2, test:news...
+++ 77453616-ffff-4dd5-b525-8d5aebc89e92, 600380fb-46a3-4cdd-9ce2-2806d6900420
+++ config: type:1000, priority:1, test:xxx <<<>>> type:1000, priority:1, test:yyy…
+++ sortedUuidToConfigMap.size 3
+++ config: type:1000, priority:1, test:xxx
+++ config: type:100, priority:2, test:news
+++ config: type:200, priority:3, test:video
根据您的比较器,当两个 UUID
存储的优先级相等时,它们是相等的。因此 77453616-ffff-4dd5-b525-8d5aebc89e92
和 600380fb-46a3-4cdd-9ce2-2806d6900420
被认为是相等的,并且只有其中一个最终出现在结果映射中。