why hashMap.toSortedMap returns one entry less

404 Views Asked by At

having a hasMap with four entries, after the toSortedMap the result map has only three entries.

var uuidToConfigMap = HashMap<UUID, Config>()

the config type:

data class Config (

    val test: String,

    val type: Int,  
    val priority: Int

) {
    override fun toString() : String {
        return "type:$type, priority:$priority"+",  test:"+test
    }
}

the data and sort code, uuidToConfigMap has four entries:

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}")
}
///

The print out shows the result sortedUuidToConfigMa has only three entries (the entry

config: type:1000, priority:1,  test:yyy  

is missing):

    +++ 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
1

There are 1 best solutions below

3
On BEST ANSWER

According to your comparator, two UUIDs are equal when stored priorities for them are equal. So 77453616-ffff-4dd5-b525-8d5aebc89e92 and 600380fb-46a3-4cdd-9ce2-2806d6900420 are considered equal and only one of them ends up in the result map.