31

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,979
  • 13
  • 45
  • 66
  • 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

5 Answers5

38

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,826
  • 2
  • 23
  • 34
  • 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
  • As you can see OP has different types for nested lists. How is this even related to the question and why people keep upvoting this? – Farid Sep 02 '22 at 15:18
  • @Farid I think the emphasis of this question is more about flattening list of lists than about list of lists of different types, as suggested by the title of the question. – DYS Sep 07 '22 at 03:26
  • @DYS Well if you answer questions by only title, then nothing to say. Content of the question is there for a reason right?! Imagine how ambiguous answers you would get if people only read the title and answered accordingly – Farid Sep 08 '22 at 13:59
20

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
  • 6,573
  • 1
  • 31
  • 54
1

You can simply try this:

fun flattenNestedList(list: List<List<Any>>?) = list?.flatten() ?: emptyList()
Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85
Mohamed Slama
  • 189
  • 1
  • 3
  • 17
0

The best way I've found to flatten lists:

val flatList = listOf<String>("A", "B", *listOf("C", "D").toTypedArray())
CamW
  • 3,223
  • 24
  • 34
0

Some year after but here some simple examples:

With lists of lists

val array = listOf(listOf("a", "b"), listOf("c", "b"))
.flatten().toTypedArray()

With arrays of arrays

val array = arrayOf(arrayOf("a", "b"), arrayOf("c", "b"))
.flatten().toTypedArray()

With lists of arrays

val array = listOf(arrayOf("a", "b"), arrayOf("c", "b"))
.flatMap { it.toList() }.toTypedArray()

hope to help someone.