I’m curious about how people use `Map` in Kotlin. ...
# announcements
p
I’m curious about how people use
Map
in Kotlin. What do y’all do? https://twitter.com/pablisc0/status/1254465101224120324
d
Umm, like...
mapOf(....)
?
👍 1
p
Yeah, I've seen a surprising amount of people using the
HashMap()
constructor and I'm curious to see how common it is :) The tweet has polling options btw
r
But you need to log in 😉
z
`mapOf`/`mutableMapOf` create a
LinkedHashMap
which preserves iteration order, which makes your code more deterministic.
HashMap
does not preserve order.
💯 2
🙂 1
p
Nice! @Zach Klippenstein (he/him) [MOD] didn’t know that one 😗 👌 Is look up the same on both? (playing devils 🥑 )
z
Good question. I haven't really looked into that before but according to Google, it seems like LHM just maintains some extra pointers, so the time complexity is the same as HM. Actual performance is gonna be slightly slower but probably not significantly for the vast majority of use cases. Uses slightly more memory too but again, probably not significant. Here's an article about exactly this: https://publicobject.com/2016/02/08/linkedhashmap-is-always-better-than-hashmap/
d
For me it depends on whether I have a use case for retaining insertion order
Moreover, at least mathematically, the iteration order in his example does not matter
I can see you might get slight variance due to floating point errors
p
This reminds me of an anecdote. Years ago, when I did not Java, I decided to use a LinkedList and my team was perplexed by why wasn't using an ArrayList. And I was like: well, we never access items by index 🤷 We are creatures of habit 😅 (possibly why ppl still use the HashMap directly in Kotlin 🙃)
g
Case with LinkedList a bit different tho, on practice ArrayList always faster, it's hard to find a real-life usecase where LinkedList in benchmark would be faster
d
it's pretty easy when you create a case with time complexity O(n) for linked list and O(n^2) for arraylist, and make N > 1000000
but yes generally arraylist is faster...
p
I guess we should define faster 😅 access time? iteration? creation? adding items? Here are some interesting reviews of both of them for addition: https://coderstower.com/2019/07/15/arraylist-vs-linkedlist-addition/ deletion: https://coderstower.com/2019/07/22/arraylist-vs-linkedlist-deletion/ more complex operations: https://coderstower.com/2019/08/06/arraylist-vs-linkedlist-sort-get-iteration/ TL;DR:
After this whole analysis about ArrayList and LinkedList, we can conclude that we should choose a list based on which use cases we are going to need.
What I read from that is: • if you are going to mutate the list, use LinkedList • if you are gonna just read sequentially it doesn’t matter much as they have pretty close in performance, specially with small lists • if you are gonna do random access (with a for loop, 🙃) then use ArrayList. 🙂
For the record, I don’t think LinkedList is better than ArrayList, just that they have different uses and understanding how they work and their limitations is important. Dogmatic believes on one being better than the other was my original problem proposition 🙂
g
Yes, for sure, if your list has 1 million elements, this us why I said "real-life"
if you are going to mutate the list, use LinkedList
Not really, only if you have huge lists with thousands elements. Because memory
if you are gonna just read sequentially it doesn't matter much as they have pretty close in performance, specially with small lists
It does matter, because LinkedList also has much higher memory usage
All those benchmarks test against 10KK lists, which is insane For such huge structures probably make sense to use more specialized data structures or some memory storage, not liststs