【问题标题】:why hashMap.toSortedMap returns one entry less为什么 hashMap.toSortedMap 少返回一个条目
【发布时间】:2018-05-13 19:53:45
【问题描述】:

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

【问题讨论】:

  • 你应该测试你的比较器。
  • 我认为它返回正常,因为如果没有第二个元素可以比较它返回一个较少的元素,请尝试删除if (config1 == null || config2 == null) { return -1 } 以检查
  • 为什么返回-1会导致少一个条目,-1只是指示顺序,对吧?
  • @OliverCharlesworth,谢谢,它正在测试比较器,如果这 4 个条目具有不同的“优先级”值,它工作正常,只有当“优先级”值存在重复时(比如这里的两个 1)它会显示问题。你发现比较器有什么问题吗?
  • 如果您能指出为什么您对这个问题投了反对票,我们将不胜感激,如果您知道答案并且可能会有很大帮助,那么您最好解释一下。

标签: android kotlin hashmap sortedmap


【解决方案1】:

根据您的比较器,当两个UUIDs 存储的优先级相等时,它们是相等的。所以77453616-ffff-4dd5-b525-8d5aebc89e92600380fb-46a3-4cdd-9ce2-2806d6900420 被认为是相等的,只有其中一个出现在结果图中。

【讨论】:

  • 非常感谢,帮了大忙!从没想过 UUID 是键,但按内部文件排序会得到这个结果。
  • 我猜是对 hashMap.toSortedMa() 如何与比较器一起工作的误解,它不像比较器仅控制排序过程中的顺序。但它也控制结果映射条目(显然在这种情况下键不一样,但比较器说它是相同的)。经验教训。
猜你喜欢
  • 2019-12-21
  • 1970-01-01
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-12
相关资源
最近更新 更多