0

So in this case I have three array that I want to map into a list of object (The objects has three parameters as well).

I have three arrays allProductCodeList, allProductNameList, and allProductQtyList (Content of this array is from a Retrofit Client response)

 allProductCodeList = response.body()?.data?.map { it?.stkProdcode }!!
 allProductNameList = response.body()?.data?.map { it?.proName }!!
 allProductQtyList = response.body()?.data?.map { it?.stkAllqty }!!

This is the content of the array I printed into the LogCat:

[![enter image description here][2]][2]

This is the Data class which I want to parse these arrays into:

data class ProcodeRecommendationListDataClass(
    val procode: String?,
    val productName: String?,
    val qty: Int?
)

What I want to do is parse these three array into a list that will looks like:

[ProcodeRecommendationListDataClass("0100009","", 2),ProcodeRecommendationListDataClass("0100061","", 1),ProcodeRecommendationListDataClasslass("0100062","", 6)]

I've done it when I only have two arrays to map (I use this solution for it). But now it I have three arrays, I confused.

If there's any detail I miss to point out, Just let me know !

0

4 Answers 4

3

1. This is you three arrays

allProductCodeList = response.body()?.data?.map { it?.stkProdcode }!!
 allProductNameList = response.body()?.data?.map { it?.proName }!!
 allProductQtyList = response.body()?.data?.map { it?.stkAllqty }!!

2. Make A New List

List<ProcodeRecommendationListDataClass> finalList = List()

3. Run a for loop with any of three array size with indices;

 for(pos in allProductCodeList.indices){

finalList.add(ProcodeRecommendationListDataClass(allProductCodeList[pos],
allProductNameList[pos],
allProductQtyList[pos] ))

}

Now finalList is your result.

Sign up to request clarification or add additional context in comments.

Comments

3

One forward straight way is to use one more zip - someone once said all problems are solved with one more level of inderection:

allProductCodeList
    .zip(allProductNameList)
    .zip(allProductQtyList)
    .map { (codeAndName, qt) ->
        ProcodeRecommendationListDataClass(
            codeAndName.first,
            codeAndName.second,
            qt
        )
    }

It doesn't look super pretty, but it should be ok.


Another way is to create your own zip that takes 2 lists:

fun <X, Y, Z, R> List<X>.zipWith(l1: List<Y>, l2: List<Z>, transform: (X, Y, Z) -> R): List<R> {
  val length = min(min(size, l1.size), l2.size)

  val result = mutableListOf<R>()
  for (i in 0 until length) {
    result.add(transform(get(i), l1[i], l2[i]))
  }

  return result
}

fun main() {
  val k = allProductCodeList.zipWith(allProductNameList, allProductQtyList) { code, name, qt ->
        ProcodeRecommendationListDataClass(
            code,
            name,
            qt
        )
    }

  println(k)
}

Basically extends a list of X that takes 2 other lists. It iterates through them applying the transform method (this is so you can map the elements as you go).

This will iterate always the smallest amount of elements - in other words, you won't get more elements than the smallest list. I can't be sure, but I assume the default implementation does something similar.

Comments

1

Why not just create objects in place?

val allProducts = response.body()?.data?.map { 
    ProcodeRecommendationListDataClass(it?.stkProdcode, it?.proName, it?.stkAllqty) 
} ?: emptyList()

7 Comments

I think this should be the accepted answer, everything done in O(n) steps and has proper handling of null-safety.
@AnimeshSahu Hmm... Isn't for loop is O(n) as well ? Or does the map function itself has bigger complexity so its the best not to use it with for loop ? Sorry i'm quite begineer in analysis like this. Well come to think of it, this answer is easier to understand for me.
No it can't be an accepted answer. Because user want to zip three separated list.
@AbhayKoradiya maybe because he has XY problem?
@dimashermanto for loop is the same but mapping 3 times and then using a for loop makes a total of O(4n) steps, and zippings produce list of pairs as well so adds up O(n) extra steps before mapping. This one is clean and uses single for loop behind the scenes.
|
0

You can use mapIndexed instead of map. use index to get third data.

val list = allProductCodeList.zip(allProductNameList)
        .mapIndexed { index, pair -> SomeClass(pair.first, pair.second,allProductQtyList[index]) }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.