简体   繁体   English

如何使用 Java 8 流迭代多级映射?

[英]How to iterate Multilevel Map using Java 8 stream?

I'm good with Java streams, so maybe this is not the minimal solution, but here's what I came up with using streams to get what I think you want:我很擅长 Java 流,所以也许这不是最小的解决方案,但这是我想出的使用流来获得我认为你想要的东西:

Do we have DishDiet as a POJO and DishDietTest as the main class?我们是否有 DishDiet 作为 POJO 和 DishDietTest 作为主类? I am getting data as desired but how to loop it?我正在根据需要获取数据,但如何循环它? How to iterate Multilevel Map using Java 8 stream?如何使用 Java 8 流迭代多级映射? How to get print based on classification done using stream?如何根据使用流完成的分类进行打印?

public class DishDiet {

    private final String name;
    private final boolean vegetarian;
    private final int calories;
    private final Type type;
    private final CaloricLevel caloricLevel;

    public DishDiet(String name, boolean vegetarian, int calories, Type type, CaloricLevel caloricLevel) {
        this.name = name;
        this.vegetarian = vegetarian;
        this.calories = calories;
        this.type = type;
        this.caloricLevel = caloricLevel;
    }

    public String getName() {
        return name;
    }

    public boolean isVegetarian() {
        return vegetarian;
    }

    public int getCalories() {
        return calories;
    }

    public Type getType() {
        return type;
    }

    public CaloricLevel getCaloricLevel() {
        return caloricLevel;
    }

    @Override
    public String toString() {
        return name;
    }

    
    public enum Type {
        MEAT, FISH, OTHER
    }

    public enum CaloricLevel {
        DIET, NORMAL, FAT
    }

}
public class DishDietTest {

    private static List<DishDiet> getAllDishDiet() {
        return Arrays.asList(new DishDiet("pork", false, 800, DishDiet.Type.MEAT, DishDiet.CaloricLevel.FAT),
                new DishDiet("beef", false, 700, DishDiet.Type.MEAT, DishDiet.CaloricLevel.FAT),
                new DishDiet("chicken", false, 400, DishDiet.Type.MEAT, DishDiet.CaloricLevel.DIET),
                new DishDiet("french fries", true, 530, DishDiet.Type.OTHER, DishDiet.CaloricLevel.NORMAL),
                new DishDiet("rice", true, 350, DishDiet.Type.OTHER, DishDiet.CaloricLevel.DIET),
                new DishDiet("season fruit", true, 120, DishDiet.Type.OTHER, DishDiet.CaloricLevel.DIET),
                new DishDiet("pizza", true, 550, DishDiet.Type.OTHER, DishDiet.CaloricLevel.NORMAL),
                new DishDiet("prawns", false, 300, DishDiet.Type.FISH, DishDiet.CaloricLevel.DIET),
                new DishDiet("salmon", false, 450, DishDiet.Type.FISH, DishDiet.CaloricLevel.NORMAL));
    }

    public static void main(String[] args) {

        Map<DishDiet.Type, Map<DishDiet.CaloricLevel, List<DishDiet>>> dishesByTypeCaloricLevel = getAllDishDiet()
                .stream().collect(groupingBy(DishDiet::getType, groupingBy((dish -> {
                    if (dish.getCalories() <= 400)
                        return DishDiet.CaloricLevel.DIET;
                    else if (dish.getCalories() <= 700)
                        return DishDiet.CaloricLevel.NORMAL;
                    else
                        return DishDiet.CaloricLevel.FAT;
                }

        ))));

        System.out.println(dishesByTypeCaloricLevel);

    }

}

for(Map.Entry<DishDiet.Type, Map<DishDiet.CaloricLevel,List>> t : dishesByTypeCaloricLevel.entrySet()){ DishDiet.Type key = t.getKey(); for(Map.Entry<DishDiet.Type, Map<DishDiet.CaloricLevel,List>> t:dishByTypeCaloricLevel.entrySet()){ DishDiet.Type key = t.getKey(); for (Map.Entry<DishDiet.CaloricLevel,List> e : t.getValue().entrySet()) System.out.println("OuterKey:" + key + " InnerKey: " + e.getKey()+ " VALUE:" +e.getValue()); for (Map.Entry<DishDiet.CaloricLevel,List> e : t.getValue().entrySet()) System.out.println("OuterKey:" + key + " InnerKey: " + e.getKey()+ " VALUE :" +e.getValue()); } }

Do you mean like this?你的意思是这样吗?

    dishesByTypeCaloricLevel.forEach((type, innerMap) 
            -> {
                System.out.println(type);
                innerMap.forEach((cl, list)
                        -> {
                            System.out.println("    " + cl);
                            list.forEach(dd -> System.out.println("        " + dd));
                        });
            });

Output:输出:

 OTHER NORMAL french fries pizza DIET rice season fruit FISH NORMAL salmon DIET prawns MEAT FAT pork NORMAL beef DIET chicken

I am not using streams for iterating and printing.我没有使用流进行迭代和打印。 I recommend that we just use the forEach methods of Map and List .我建议我们只使用MapListforEach方法。 The use is similar but a bit simpler.用法类似,但更简单一些。

Do we have DishDiet as a POJO and DishDietTest as the main class?我们是否有 DishDiet 作为 POJO 和 DishDietTest 作为主类?

Yes, that's fine.是的,没关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM