Я новичок в Java 8 и ищу здесь что-нибудь интересное.На самом деле хотел получить Самую высокую калорийность блюда от каждого Dish.Type
.
Я попробовал кое-что ниже, но это дает мне все значения из Dish.Type.
@Builder
@Data
@AllArgsConstructor
public class Dish {
public enum Type { MEAT, FISH, OTHER }
private final String name;
private final boolean vegetarian;
private final int calories;
private final Type type;
public static final List<Dish> menu =
Arrays.asList( new Dish("pork", false, 800, Dish.Type.MEAT),
new Dish("beef", false, 700, Dish.Type.MEAT),
new Dish("chicken", false, 400, Dish.Type.MEAT),
new Dish("french fries", true, 530, Dish.Type.OTHER),
new Dish("rice", true, 350, Dish.Type.OTHER),
new Dish("season fruit", true, 120, Dish.Type.OTHER),
new Dish("pizza", true, 550, Dish.Type.OTHER),
new Dish("prawns", false, 400, Dish.Type.FISH),
new Dish("salmon", false, 450, Dish.Type.FISH));
}
В поисках результата - получите наивысшую калорийность блюда для каждого типа блюда.
Я попробовал ниже, но приведу все элементы одного и того же типа блюда.Есть указатели?
List<Dish> truncatingStream = Dish.menu.stream().filter(d -> d.getCalories() > 300).limit(3).collect(toList());
truncatingStream.forEach(System.out::println);