Chih Wei Lin
03/05/2022, 5:10 PMval targetKeywords = listOf("red", "blue", "yellow", "white", "black")
val inputData = listOf(
"one blue",
"two gray",
"three black",
"four blue",
"blue",
"five yellow",
"six gray",
"yellow",
"seven black",
"nine gray",
"ten blue",
"purple"
)
val resultMap = targetKeywords.associateWith { mutableListOf<String>() }
inputData.forEach { item ->
targetKeywords.forEach { color ->
if (item.contains(color)) {
resultMap[color]!! += item
return@forEach
}
}
}
print(resultMap)
// result: {red=[], blue=[one blue, four blue, blue, ten blue], yellow=[five yellow, yellow], white=[], black=[three black, seven black]}
Vampire
03/05/2022, 9:09 PMval resultMap = targetKeywords.associateWith { color ->
inputData.filter { item -> color in item }
}
?Chih Wei Lin
03/06/2022, 5:15 AMRoukanken
03/07/2022, 10:38 AMVampire
03/07/2022, 11:51 AM