oday
11/07/2019, 10:28 AMjwinwood
11/07/2019, 10:33 AMSingle<HashMap<String, List<Product>>>
, toMap
will return the Kotlin stdlib Map interface. HashMap is based on Java collection, and the Kotlin compiler won't know how to cast a Kotlin Map to a HashMapoday
11/07/2019, 10:34 AMCollections....
before the datatypejwinwood
11/07/2019, 10:34 AMSingle<Map<String, List<Products>>>
then toMap
should work?oday
11/07/2019, 10:34 AMjwinwood
11/07/2019, 10:35 AMmutableMapOf
oday
11/07/2019, 10:37 AModay
11/07/2019, 10:37 AMjwinwood
11/07/2019, 10:37 AMclass Repository(
private val apiService: ApiService
) : IRepository {
override fun getProducts(): Single<Map<String, List<Product>>> =
apiService.getProducts().map { productListings ->
productListings.groupBy {it.name}.toMap()
}
}
Should that not suffice?oday
11/07/2019, 10:38 AModay
11/07/2019, 10:38 AModay
11/07/2019, 10:41 AMjwinwood
11/07/2019, 10:43 AMclass Repository(
private val apiService: ApiService
) : IRepository {
override fun getProducts(): Single<Map<String, List<Product>>> =
apiService.getProducts().map { productListings ->
val productsMap = mutableMapOf<String, List<Product>>()
productListings.groupBy {it.name}
productListings.foreach { listing ->
productsMap.put(listing.name, listing.product)
}
productsMap
}
}
I'm sure there is a more functional way of doing it but I think this should workoday
11/07/2019, 10:52 AMjwinwood
11/07/2019, 10:54 AModay
11/07/2019, 10:57 AModay
11/07/2019, 10:57 AMwbertan
11/07/2019, 11:02 AMdata class Asas(val id: Int, val name: String)
val list = listOf<Asas>()
val asas: HashMap<String, List<Asas>> = HashMap(list.groupBy { it.name })
Can create a HashMap
from the groupBy
?! (Not tested the code above)