A list of pairs is just a list of paired values. A...
# getting-started
r
A list of pairs is just a list of paired values. A map is a mapping from keys to values, so you can for example lookup values by keys. (Sorry to use the names in the descriptions, but in my defense, they are aptly named)
Copy code
val people = mapOf("a" to Person("Fred"), "b" to Person("George"))
val fred = people["a"]
s
Strictly speaking, you could still technically “look up” values by keys in the
List<Pair>
structure. It’s a terrible idea but it’s possible.
Copy code
val fred = people.first { it.first = "a" }.second
It’s really the only reason I’d consider the two constructs even comparable for this kind of question 😕
r
That's not really a lookup though, just a list operation.
💯 1
The difference may seem pedantic, but the semantic meaning carries a lot of weight.
👍 1
s
I mean, you and I understand that, but to a lot of people it’ll achieve the goal they’ve set. I put
"look up"
in quotes for a good reason - you’re right that the difference is very important. I just don’t know why else someone would compare the two.
r
Fair enough, I see where you're coming from.
s
Edited my example to be more in line with your snippet, to show a naive “key-based lookup”, heavy on the quotation marks lol
👍 1
f
Thanks guys. The sample using first {} is obscure to me but actually the difference is pretty clear and I wonder why I got confused. Probably because the use of the same "to", function, although it yielded significantly different results according to the case.
a
also, note that the list can contain the same "key" more than once and has order, not all maps guarantee to maintain their items sequence order.
✔️ 3
a
@febs note that the
mapOf
function takes
vararg of pairs
to construct a map - perhaps that's where the confusion stems from? 🙂
👍 1
Definition:
fun <K, V> mapOf(vararg pairs: Pair<K, V>): Map<K, V>