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?
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 ...
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.
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.
List and array are two popular collections supported by Kotlin. By definition, both these collections allocate sequential memory location.
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.
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.
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.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With