代码拉取完成,页面将自动刷新
package com.lun.c06;
import java.util.*;
import java.util.function.*;
import static java.util.stream.Collectors.*;
import static com.lun.c06.Dish.menu;
public class Summarizing {
public static void main(String ... args) {
System.out.println("Nr. of dishes: " + howManyDishes());
System.out.println("The most caloric dish is: " + findMostCaloricDish());
System.out.println("The most caloric dish is: " + findMostCaloricDishUsingComparator());
System.out.println("Total calories in menu: " + calculateTotalCalories());
System.out.println("Average calories in menu: " + calculateAverageCalories());
System.out.println("Menu statistics: " + calculateMenuStatistics());
System.out.println("Short menu: " + getShortMenu());
System.out.println("Short menu comma separated: " + getShortMenuCommaSeparated());
}
private static long howManyDishes() {
return menu.stream().collect(counting());
}
private static Dish findMostCaloricDish() {
return menu.stream().collect(reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2)).get();
}
private static Dish findMostCaloricDishUsingComparator() {
Comparator<Dish> dishCaloriesComparator = Comparator.comparingInt(Dish::getCalories);
BinaryOperator<Dish> moreCaloricOf = BinaryOperator.maxBy(dishCaloriesComparator);
return menu.stream().collect(reducing(moreCaloricOf)).get();
}
private static int calculateTotalCalories() {
return menu.stream().collect(summingInt(Dish::getCalories));
}
private static Double calculateAverageCalories() {
return menu.stream().collect(averagingInt(Dish::getCalories));
}
private static IntSummaryStatistics calculateMenuStatistics() {
return menu.stream().collect(summarizingInt(Dish::getCalories));
}
private static String getShortMenu() {
return menu.stream().map(Dish::getName).collect(joining());
}
private static String getShortMenuCommaSeparated() {
return menu.stream().map(Dish::getName).collect(joining(", "));
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。