Home > Back-end >  How to add two equal sized lists into one list with key pairs
How to add two equal sized lists into one list with key pairs

Time:11-03

In Android Studio, I have two lists, specifically:

val distanceResults = Pair(mutableListOf<String>(), floatArrayOf())

It's a pair where each item is its own list; these sublists will always be the same length.

What I need to do is take each index of each list and merge them into one list. So that the end result would look something like:

    list1 -> ("A", "B", "C")
    list2 -> (1F, 2F, 3F)
    finalList -> (
        ["A", 1F],
        ["B", 2F],
        ["C", 3F],
    )

I'm not sure if a filter, flatFiler, map, or something else would do the trick.

CodePudding user response:

You can zip them, but first you must convert the FloatArray to be iterable so they can be combined. (The standard library doesn't have overloads of all the iterable operators to work with all the different types of arrays.)

val finalList = distanceResults.first zip distanceResults.second.asIterable()
  • Related