android - kotlin 是否有用于从两种不同类型的列表中获取公共数据的高阶函数?
问题描述
我有与此链接中描述的相同的问题(但它很快) 从两种不同类型的数组中获取公共数据
我努力了 :
val list=ArrayList<Model>()
val list1=ArrayList<Model1>()
val hashMap=Hashmap<Int,Int>()
for (i in list.indices) {
val data = list1.filter { it.name == list[i].name }
if (data.isNotEmpty()) {
hashMap.put(data[0].id,list[i].id)
}
}
解决方案
编辑:根据下面的评论,这是一种无需使用谓词迭代两次的方法:
val map = list.mapNotNull{el ->
list1.firstOrNull{el1 -> el.name == el1.name}?.let{el1 -> el.id to el1.id}
}.toMap()
推荐阅读
- swift - 如何访问第一组数组?SwiftyJSON
- node.js - Node.js 在处理大型并发请求时出错
- matlab - 如何以更有效的方式创建/索引此矩阵?
- rest - 如何使用 Web API 从 Dynamics 365 返回 50 多个结果
- php - 包含从出生到今年的每个日期的生日日期和对应天数的网页
- java - 具有复杂正则表达式的 xsd 限制
- r - 如何使用 ggplot2 在小提琴图上显示最高密度点的 Y 值?
- php - 获取用户表中的名称字段
- sql-server-2012 - SSIS 2012 在 64 位系统上从 SSISDB 执行包
- javascript - 在 Javascript 二维数组上获取类型错误