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!
Your article helped me a lot, is there any more related content? Thanks!
This is really interesting, You’re a very skilled blogger. I’ve joined your feed and look forward to seeking more of your magnificent post. Also, I’ve shared your site in my social networks!
Pretty! This has been a really wonderful post. Many thanks for providing these details.
For the reason that the admin of this site is working, no uncertainty very quickly it will be renowned, due to its quality contents.
Your article helped me a lot, is there any more related content? Thanks! https://accounts.binance.com/en-ZA/register?ref=JHQQKNKN