21

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?

user990635
  • 3,689
  • 13
  • 42
  • 65
  • Possible duplicate of [Divide elements on groups in RecyclerView or Grouping Recyclerview items ,say by date](https://stackoverflow.com/questions/41447044/divide-elements-on-groups-in-recyclerview-or-grouping-recyclerview-items-say-by) – Hardik Chauhan Aug 05 '19 at 10:48
  • @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! – user990635 Aug 05 '19 at 10:53
  • what is `item` in the flattened list? It does not seem to appear in the `List` – leonardkraemer Aug 05 '19 at 10:57
  • @leonardkraemer - Basically if my List 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 Aug 05 '19 at 11:01
  • I edited to make it more clear – user990635 Aug 05 '19 at 11:08

3 Answers3

28

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]
DYS
  • 2,459
  • 2
  • 22
  • 32
  • 2
    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. – cs_pupil Feb 27 '20 at 20:00
16

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.

leonardkraemer
  • 5,985
  • 1
  • 26
  • 50
2

You can simply try this:

fun flattenNestedList(list: List<List<Any>>?) = list?.flatten() ?: emptyList()
Ege Kuzubasioglu
  • 5,608
  • 11
  • 45
  • 79
Mohamed Slama
  • 177
  • 1
  • 3
  • 16