Klitos Kyriacou
07/01/2022, 4:45 PMMap<Int, String>
. Each map has only a small handful of entries. I need to keep many thousands of these maps in memory. 99% of the time the Int
key is small, between 1 an 10. If it was guaranteed to always be that small, the most efficient implementation of such a map would use an Array<String?>
. However, 1% of the time there would be a key that would be a very large integer, so a simple array is unusable. Yet if I use a HashMap
or TreeMap
I would be wasting memory for 99% of the time by using a sub-optimal implementation. Is there any specialized Map that uses a strategy of changing internal implementation depending on the value of the key?Ruckus
07/01/2022, 4:52 PMRuckus
07/01/2022, 4:57 PMclass Entry(val index: Int, val value: String)
). If the number of entries is small, a simple linear search for access should be good enough. Otherwise, you could manually keep it sorted by index
and do a binary search.mkrussel
07/01/2022, 5:08 PMYoussef Shoaib [MOD]
07/02/2022, 2:13 PMKlitos Kyriacou
07/04/2022, 7:50 AMYoussef Shoaib [MOD]
07/04/2022, 9:24 AM