-2

I create Map <String,List<Object>>

 val groupListByUserName = sharedList.groupBy { it -> it.user.displayName }

this function groupBy list by userNAme. Next I create model

data class SharedList(val userName:String,val sharedList: List<MovieMyList>)

I want add data with map to list and I don't konw how. Do you have any idea how make this?

  • 5
    Hi! I think that I can't understand what you want to achieve. Could you provide an example? In addiction, you should post some experiments you made to achieve your goal. https://stackoverflow.com/help/how-to-ask – Iván Rodríguez Torres Feb 06 '19 at 14:26
  • Possible duplicate of [Convert a Map to a POJO](https://stackoverflow.com/questions/16428817/convert-a-mapstring-string-to-a-pojo) – nircraft Feb 06 '19 at 14:44

1 Answers1

1

If I understood correctly, you want to convert the Map<String, List<MovieMyList>> into a List<SharedList>, is that it?

val groupListByUserName = sharedList.groupBy { it -> it.user.displayName }
val sharedLists = groupListByUserName.map { (user, movies) -> SharedList(user, movies) }

Note that here, the parentheses in the lambda are important: calling map on a Map will deal with the map's entries, each composed of a key and a value. Using the parentheses performs destructuring on the entry to directly access its key and value.

Joffrey
  • 32,348
  • 6
  • 68
  • 100
  • you can omit `it ->`: `.groupBy { it.user.displayName }` :) – Willi Mentzel Feb 06 '19 at 22:04
  • 1
    @WilliMentzel Yes of course, I just copied the OP's line as a reminder, in order to show what he has and what to do from there. That's why I prefer to keep it as-is. – Joffrey Feb 06 '19 at 22:06