【问题标题】:Android Kotlin Map Multiple Array into a list of objectAndroid Kotlin 将多个数组映射到对象列表中
【发布时间】:2020-06-25 07:34:18
【问题描述】:

所以在这种情况下,我有三个数组,我想将它们映射到一个对象列表中(对象也有三个参数)。

我有三个数组 allProductCodeListallProductNameListallProductQtyList(此数组的内容来自 Retrofit Client 响应)

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


 //I printed these arrays to LogCat so it is easier to see
 Log.i("Order", allProductCodeList.toString())
 Log.i("Order", allProductNameList.toString())
 Log.i("Order", allProductQtyList.toString())

这是我打印到 LogCat 中的数组内容:

这是我想将这些数组解析成的 Data 类:

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

我想要做的是将这三个数组解析成一个列表,如下所示:

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

当我只有两个要映射的数组时,我已经完成了(我使用this 解决方案)。但是现在我有三个数组,我很困惑。

如果有任何我想指出的细节,请告诉我!

【问题讨论】:

    标签: android arrays kotlin


    【解决方案1】:

    1.这是你们三个数组

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

    2。制作新清单

    List<ProcodeRecommendationListDataClass> finalList = List()
    

    3.使用 indices;

    运行三个数组大小中的任何一个的 for 循环
     for(pos in allProductCodeList.indices){
    
    finalList.add(ProcodeRecommendationListDataClass(allProductCodeList[pos],
    allProductNameList[pos],
    allProductQtyList[pos] ))
    
    }
    

    现在 finalList 是您的结果。

    【讨论】:

      【解决方案2】:

      一种直接的方法是多使用一个zip - 有人曾经说过,所有问题都可以通过多一层的粗心大意来解决:

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

      看起来不是很漂亮,但应该没问题。


      另一种方法是创建您自己的包含 2 个列表的 zip:

      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)
      }
      

      基本上扩展了X 的列表,它需要另外两个列表。它应用transform 方法对它们进行迭代(这样您就可以随时映射元素)。

      这将始终迭代最少数量的元素 - 换句话说,您不会得到比最小列表更多的元素。我不能确定,但​​我认为默认实现会做类似的事情。

      【讨论】:

        【解决方案3】:

        为什么不就地创建对象?

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

        【讨论】:

        • 我认为这应该是公认的答案,一切都在 O(n) 步骤中完成,并且正确处理了 null-safety。
        • @AnimeshSahu 嗯... for 循环不也是 O(n) 吗?或者 map 函数本身是否具有更大的复杂性,所以最好不要将它与 for 循环一起使用?对不起,我在这样的分析方面很初学者。好吧,想想看,这个答案对我来说更容易理解。
        • 不,这不能是一个公认的答案。因为用户想要压缩三个分开的列表。
        • @AbhayKoradiya 可能是因为他有XY problem
        • @dimashermanto for 循环是相同的,但映射 3 次,然后使用 for 循环总共需要 O(4n) 步,并且压缩也会产生对列表,所以加起来 O(n)映射前的额外步骤。这个很干净,在幕后使用单个 for 循环。
        【解决方案4】:

        您可以使用mapIndexed 代替地图。使用索引获取第三个数据。

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

        【讨论】:

          猜你喜欢
          • 2019-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-18
          • 1970-01-01
          • 2021-09-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多