https://kotlinlang.org logo
Title
r

Ruckus

01/22/2019, 9:10 PM
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)
val people = mapOf("a" to Person("Fred"), "b" to Person("George"))
val fred = people["a"]
s

Shawn

01/22/2019, 9:13 PM
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.
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

Ruckus

01/22/2019, 9:14 PM
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

Shawn

01/22/2019, 9:16 PM
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

Ruckus

01/22/2019, 9:16 PM
Fair enough, I see where you're coming from.
s

Shawn

01/22/2019, 9:17 PM
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

febs

01/22/2019, 9:46 PM
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

Alan Evans

01/22/2019, 9:47 PM
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

arve

01/23/2019, 12:08 AM
@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>