Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flatten List of Lists in Kotlin?

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?

like image 520
user990635 Avatar asked Aug 05 '19 10:08

user990635


People also ask

What is flatMap Kotlin?

The flatMap is one of the features, and it is used to combine with two different collections, and it returns with the collection with the single list of all elements yielded and retrieved data from the results with applying certain conditions to check on each element of the original collection the elements/items are ...

How do you use Kotlin reduce?

Accumulates value starting with the first element and applying operation from left to right to current accumulator value and each element. Throws an exception if this array is empty. If the array can be empty in an expected way, please use reduceOrNull instead.

How do I create a list of strings in Kotlin?

Inside main() , create a variable called numbers of type List<Int> because this will contain a read-only list of integers. Create a new List using the Kotlin standard library function listOf() , and pass in the elements of the list as arguments separated by commas.

Is a list an array in Kotlin?

List and array are two popular collections supported by Kotlin. By definition, both these collections allocate sequential memory location.

How to define a list of lists in Kotlin?

To define a list of Lists in Kotlin, call listOf()function and provide other Lists as elements to this function. The syntax of listOf() function is fun <T> listOf(vararg elements: T): List<T> listOf() function returns a read-only List.

How do I create a list with duplicate elements in Kotlin?

Kotlin mutable or immutable lists can have duplicate elements. For list creation, use the standard library functions listOf () for read-only lists and mutableListOf () for mutable lists.

How do I create a mutable list in Kotlin?

Kotlin mutable or immutable lists can have duplicate elements. For list creation, use the standard library functions listOf () for read-only lists and mutableListOf () for mutable lists. To prevent unwanted modifications, obtain read-only views of mutable lists by casting them to List.

What is the difference between indexOf() and get() methods in Kotlin?

The indexOf () method returns the index of the first occurrence of the specified element in the list, or -1 if the specified element is not contained in the list. When you run the above Kotlin program, it will generate the following output: The get () method can be used to get the element at the specified index in the list.


1 Answers

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]
like image 193
DYS Avatar answered Sep 30 '22 19:09

DYS