https://kotlinlang.org logo
Title
m

mcscruff

08/10/2017, 10:46 AM
im a little stuck, i have a mutable map with Names as a key and address's as a value, is it possible to "get" the key and value at any given index. For example, if i wanted to know the 6th in the map
d

diesieben07

08/10/2017, 10:51 AM
Usually maps do not specify an order, and if they do (e.g.
LinkedHashMap
) access to the n-th element is usually quite slow. This is probably not a good idea to do 😉
👍 1
m

mannodermaus

08/10/2017, 10:52 AM
LinkedHashMap’s get is O(1) so that’s not an issue thankfully!
d

diesieben07

08/10/2017, 10:52 AM
get
is, yes. But "find me the 6th element" is O(n)
👍 1
m

mannodermaus

08/10/2017, 11:01 AM
Ah yes, you’re right
d

diesieben07

08/10/2017, 11:02 AM
But that performance does not even matter, unless your map is quite big. I just think it's a bit of a code smell.
👍 1
m

marstran

08/10/2017, 11:57 AM
@mcscruff Maybe you would be better off with a class containing a name and an address, and then store them in a list?
m

mcscruff

08/10/2017, 11:59 AM
im actually looking at a class now, just working out how to use it correctly
👍 1
i got it working, a class is a much better way to do it.