How to flatten List of Lists in Kotlin?

22,514

Solution 1

As answered by @DYS you can and should use flatten to flatten a list. My answer covers how to achieve the special case stated in the question.

You can do it this way:

val a = listOf(
    AA(LocalDate.now(), listOf(BB(1, "1", "1")))
)
val flattened = a.flatMap { aa -> mutableListOf<Any>(aa.date).also { it.addAll(aa.bb) }}

see complete example

Basically you use flatMap, create a MutableList<Any> with the date and then addAll items of BB in the also block. Probably there is a more elegant way to do it but this one came to me first.

Simply using flatten does not work here, because AA does not implement iterable.

Solution 2

The simplest one I know of is the extension flatten() function to Iterable. Since List is a subclass of the latter, it is applicable.

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/flatten.html

val deepList = listOf(listOf(1), listOf(2, 3), listOf(4, 5, 6))
println(deepList.flatten()) // [1, 2, 3, 4, 5, 6]

Solution 3

You can simply try this:

fun flattenNestedList(list: List<List<Any>>?) = list?.flatten() ?: emptyList()
Share:
22,514

Related videos on Youtube

user990635
Author by

user990635

Updated on July 09, 2022

Comments

  • user990635
    user990635 almost 2 years

    I have a list of objects of class AA that contain a date and a list of objects of class BB:

    data class AA(
        val date: LocalDate,
        val bb: List<BB>
    )
    
    @Parcelize
    data class BB(
        val x: Int,
        val y: String,
        val z: String
    ) : Parcelable
    

    I want to create a single List (flatten List<AA>) that will look like this:

     listOf(
        date obj
        BB obj
        BB obj
        date obj
        BB obj
        date obj
        BB obj
        BB obj 
        BB obj)
    

    Instead of:

     listOf(
        date obj, listOf(BB obj, BB obj)
        date obj, listOf(BB obj)
        date obj, listOf(BB obj, BB obj, BB obj))
    

    I tried using flatMap, but I only manage to flatten one part - BB.
    How to crate a list with date and BB items?

    • Hardik Chauhan
      Hardik Chauhan almost 5 years
    • user990635
      user990635 almost 5 years
      @Hardik Chauhan - My question was how to flatten the list. The RecyclerView is what it will be used for and is not relevant (I removed it). Anyway the answer there is about grouping. I need to represent existing objects. So it's not a duplicate question!
    • leonardkraemer
      leonardkraemer almost 5 years
      what is item in the flattened list? It does not seem to appear in the List<AA>
    • user990635
      user990635 almost 5 years
      @leonardkraemer - Basically if my List<AA> looks like that: listOf((date1,listOf(2,a,b), (date2,listOf((3,v,d),(5,c,j))) etc. Then I want a list: listOf(date1,(2,a,b),date2,(3,v,d),(5,c,j))
    • user990635
      user990635 almost 5 years
      I edited to make it more clear
  • cs_pupil
    cs_pupil about 4 years
    You can also do this: listOf(1) + listOf(2,3) + listOf(4,5,6) (which is shorthand for: listOf(1).plus(listOf(2,3)).plus(listOf(4,5,6))) to get the same results.