我很擅長 Java 流,所以也許這不是最小的解決方案,但這是我想出的使用流來獲得我認為你想要的東西:
我們是否有 DishDiet 作為 POJO 和 DishDietTest 作為主類?我正在根據需要獲取資料,但如何回圈它?如何使用 Java 8 流迭代多級映射?如何根據使用流完成的分類進行列印?
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);
}
}
uj5u.com熱心網友回復:
你的意思是這樣嗎?
dishesByTypeCaloricLevel.forEach((type, innerMap)
-> {
System.out.println(type);
innerMap.forEach((cl, list)
-> {
System.out.println(" " cl);
list.forEach(dd -> System.out.println(" " dd));
});
});
輸出:
OTHER NORMAL french fries pizza DIET rice season fruit FISH NORMAL salmon DIET prawns MEAT FAT pork NORMAL beef DIET chicken
我沒有使用流進行迭代和列印。我建議,我們只是使用forEach的方法Map和List。用法類似,但更簡單一些。
我們是否有 DishDiet 作為 POJO 和 DishDietTest 作為主類?…
是的,沒關系。
uj5u.com熱心網友回復:
for(Map.Entry<DishDiet.Type, Map<DishDiet.CaloricLevel,List<DishDiet>>> t : dishesByTypeCaloricLevel.entrySet()){
DishDiet.Type key = t.getKey();
for (Map.Entry<DishDiet.CaloricLevel,List<DishDiet>> e :
t.getValue().entrySet())
System.out.println("OuterKey:" key " InnerKey: " e.getKey() " VALUE:" e.getValue());
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/352457.html
下一篇:組/樞軸字典Python
