ListからMapに変換したいのだが、forとかで回すとかじゃなくて関数型でうまいこと変換できないかを探したら見つけたのでメモ。
安定のstack overflowに答えがあった。
cf: How to convert List to Map in Kotlin?
上記によるとどうやら2通りの方法があるようだ。
一つはassociateByを使う方法
val map = friends.associateBy({it.facebookId}, {it.points})
もう一つはtoMapを使う方法
val map = friends.map { it.facebookId to it.points }.toMap()
associateByのほうがなんかスマート感ある
関数型をうまく使ってきれいにコードがかけるのでガンガン使っていきたいです。
こんにちは。virapture…
View Comments
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://accounts.binance.com/ur/register?ref=P9L9FQKY
Your article helped me a lot, is there any more related content? Thanks!