Kotlin实现:将Item列表分类为MEAT/VEG为键的Map<List<Item>>
Kotlin等效实现:按食材分类Item到Map中
需求说明
遍历Item列表,判断Item是否包含肉类或鸡蛋,将其存入以TYPE.MEAT/TYPE.VEG为键的Map对应列表中,不符合条件的Item直接跳过。
原始Java代码
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class FoodCategorizer { enum TYPE { MEAT, VEG } static class Item { String name; List<String> ingredients; public Item(String name, List<String> ingredients) { this.name = name; this.ingredients = ingredients; } } private static boolean containsMeat(Item item) { // 此处为判断是否含肉类的逻辑示例 return item.ingredients.contains("chicken"); } private static boolean containsEgg(Item item) { // 此处为判断是否含鸡蛋的逻辑示例 return item.ingredients.contains("egg"); } public static Map<TYPE, List<Item>> categorizeItems(List<Item> foodList) { final Map<TYPE, List<Item>> itemtypeToContent = new HashMap<>(); itemtypeToContent.put(TYPE.MEAT, new ArrayList<>()); itemtypeToContent.put(TYPE.VEG, new ArrayList<>()); foodList.forEach(item -> { if (containsMeat(item)) { itemtypeToContent.get(TYPE.MEAT).add(item); } else if (containsEgg(item)) { itemtypeToContent.get(TYPE.VEG).add(item); } else { // 记录无效Item - 跳过 } }); return itemtypeToContent; } }
输入示例
val foodList = listOf( Item("Omlette", listOf("egg", "onion", "salt", "pepper", "mushroom")), Item("ScrambledEggs", listOf("egg", "milk", "salt", "pepper")), Item("ChickenTacos", listOf("chicken", "onion", "salt")), Item("Fries", listOf("potatoes", "salt")) )
注:Fries因不含肉类或鸡蛋被跳过
输出示例
mapOf( TYPE.MEAT to listOf(Item("ChickenTacos", listOf("chicken", "onion", "salt"))), TYPE.VEG to listOf( Item("Omlette", listOf("egg", "onion", "salt", "pepper", "mushroom")), Item("ScrambledEggs", listOf("egg", "milk", "salt", "pepper")) ) )
Kotlin实现方案
简洁函数式版本(推荐)
利用Kotlin集合API的链式操作,通过mapNotNull过滤无效Item并绑定分类,再用groupBy完成分组:
enum class TYPE { MEAT, VEG } data class Item(val name: String, val ingredients: List<String>) fun containsMeat(item: Item): Boolean { // 替换为实际判断逻辑 return item.ingredients.contains("chicken") } fun containsEgg(item: Item): Boolean { // 替换为实际判断逻辑 return item.ingredients.contains("egg") } fun categorizeItems(foodList: List<Item>): Map<TYPE, List<Item>> { return foodList.mapNotNull { item -> when { containsMeat(item) -> TYPE.MEAT to item containsEgg(item) -> TYPE.VEG to item else -> null // 跳过不符合条件的Item } }.groupBy(keySelector = { it.first }, valueTransform = { it.second }) // 若需确保Map始终包含MEAT和VEG键(即使对应列表为空),取消下方注释: // .toMutableMap().apply { // TYPE.values().forEach { putIfAbsent(it, emptyList()) } // } }
类Java风格的遍历版本
如果更习惯命令式写法,可使用Kotlin的可变集合完成类似逻辑:
fun categorizeItemsImperative(foodList: List<Item>): Map<TYPE, List<Item>> { val itemTypeToContent = mutableMapOf<TYPE, MutableList<Item>>().apply { put(TYPE.MEAT, mutableListOf()) put(TYPE.VEG, mutableListOf()) } foodList.forEach { item -> when { containsMeat(item) -> itemTypeToContent[TYPE.MEAT]?.add(item) containsEgg(item) -> itemTypeToContent[TYPE.VEG]?.add(item) else -> { // 记录无效Item - 跳过 } } } return itemTypeToContent.toMap() // 转换为不可变Map返回(可选) }
说明
- 函数式版本更贴合Kotlin惯用写法,代码简洁且可读性强
- 若业务要求返回的Map必须包含
MEAT和VEG键(即使对应列表为空),启用函数式版本中注释的代码即可 data class Item自动生成equals、toString等实用方法,比Java普通类更高效
内容的提问来源于stack exchange,提问作者meso
相关产品推荐
相关产品推荐

