Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Covert list to Map with list in Kotlin

This might be a common question, but I did not find anything specific to what I am looking for. Also, I am newbie to Kotlin.

Here is my existing code that I want to simplify in Kotlin. The below code is in Java and I am looking for Kotlin equivalent of it

Travserse through the list of items, identify if it contains MEAT or EGG and accordingly put it in a Map with a LIST. The key to MAP is MEAT/VEG.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

final Map<TYPE, List<String>> 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 {
                // log invalid item - skip
            }
        });
        return itemtypeToContent;



enum TYPE {
MEAT,
VEG
}

class Item {
String name,
List<String> ingredients
}

input:

List<Item> foodList = {{Omlette, {egg, onion, salt, pepper, mushroom}},
                       {ScrambledEggs, {egg, milk, salt, pepper}},
                       {ChickenTacos, {chicken, onion, salt}}
                       {Fries, {potatoes, salt}}}

output:

Map {MEAT, [{ChickenTacos, {chicken, onion, salt}}]
     VEG,  [{Omlette, {egg, onion, salt, pepper, mushroom}},
            {ScrambledEggs, {egg, milk, salt, pepper}}]}

We skipped FRIES because it did not contain meat or egg

>Solution :

You basically want to categorise dishes based on whether they contain meat or not, so assuming you only have 2 categories you can do it like this

fun main() {
    val dishes = listOf(
        Dish("Omelette", listOf("egg", "onion", "salt", "pepper", "mushroom")),
        Dish("ChickenTacos", listOf("chicken", "onion", "salt")),
        Dish("ScrambledEggs", listOf("egg", "milk", "salt", "pepper")),
        Dish("Fries", listOf("potatoes", "salt"))
    )

    val grouped = dishes.groupBy {
        when {
            containsMeat(it) -> IngredientType.MEAT
            containsVeg(it) -> IngredientType.VEG
            else -> null
        }
    }
        .filterKeys { it != null }


    println(grouped)
}

This prints:

{
    VEG=[Dish(name=Omelette, ingredients=[egg, onion, salt, pepper, mushroom]), Dish(name=ScrambledEggs, ingredients=[egg, milk, salt, pepper])],
    MEAT=[Dish(name=ChickenTacos, ingredients=[chicken, onion, salt])]
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading