Hi im trying to build a `Map<MyDo, List<MyDO...
# android
t
Hi im trying to build a
Map<MyDo, List<MyDO>>
from my local database table which has the following columns
K 1
the tables rows consist of both the parent categories (with parent_category = NULL) and the child categories i wish to build a map that has a key set of parent categories with an associated map value of a list of all the parents child categories my sql query simply returns a list of all rows contained in the db table and i want to construct the map using kotlin available list functions such as sortedBy, groupBy etc... all my attempts so far have failed. and all i can manage is to build a map with a single unique entry in the list of children is it possible to construct the desired map using kotlin functions alone?
@kenkyee might be worth letting me ask my question before flagging as not kotlin
e
I hope MyDo are the data classes or classes that have proper equals implemented. You can do it with SQL. But with Kotlin it will be something like
Copy code
val rows = <get list of rows from table>
val map = rows.map{ mydo -> 
   mydo to rows
            .filter{ it.parent_category == mydo.category_id }
}
.filter{ pair -> pair.second.isNotEmpty() }
.toMap()
🙌 1