Hey I have data coming from server. I have Topic data class inside that I have Categories class, basically I want to store some value. I did this successfully and I want to know is there any better way of doing that in terms of performance, storage, time and space complexity in kotlin idiomatic way. Can someone guide me on this.
I added
SubTopicdata class dummy is very small amount, but in actually in big size of list is coming from server.
main.kt
fun main() {
val response = createData()
response.items?.let { categories ->
val filteredList = categories.filter { it.categories != null }
.flatMap { flatCategories ->
flatCategories.categories!!.forEach { eachCategories ->
eachCategories.subTopic?.forEach {
it.priceId = flatCategories.id
}
}
flatCategories.categories
}
filteredList.forEach {
println(it)
}
}
}
fun createData() = ResponseClass(
listOf(
Topic(
"xyz",
"vivek",
listOf(
Categories("21", "physical", listOf(SubTopic("1", "abc"), SubTopic("2", "bjhef"))),
Categories("2211", "mind", listOf(SubTopic("1", "abc"), SubTopic("2", "bjhef"))),
Categories("22131", "motorized", listOf(SubTopic("1", "abc"), SubTopic("2", "bjhef"))),
Categories("2134124", "coordination", listOf(SubTopic("1", "abc"), SubTopic("2", "bjhef"))),
Categories("211243", "animal-supported", listOf(SubTopic("1", "abc"), SubTopic("2", "bjhef"))),
)
)
)
)
ResponseClass.kt
data class ResponseClass(
val items: List<Topic>? = null
)
Topic.kt
data class Topic(
val id: String? = null,
val name: String? = null,
val categories: List<Categories>? = null
)
Categories.kt
data class Categories(
val id: String? = null, val title: String? = null, val subTopic: List<SubTopic>? = null
)
SubTopic.kt
data class SubTopic(
val id: String? = null, val title: String? = null, var priceId: String? = null
)