How to get a value from map in kotlin?

Viewed 3213

Is it possible to get the values in a map associated to a key, without using the key?

I have this code.

val map = prices.associateBy({it.productName},{it.productPrice})

which gives me this

{Coffee=3.0, Gum=0.5, Beer=18.0}

I want to be able to just grab all the prices (3.0, 0.5, etc.) and save them to a list.

Any help is appreciated!

1 Answers

So to get all the values from a map, you can just use the built-in values property on the map like this.

val map = prices.associateBy({it.productName},{it.productPrice})
val values = map.values

It returns a read-only Collection of all values in this map. Note that this collection may contain duplicate values.

Related