List of usage examples for java.util.stream Collectors reducing
public static <T> Collector<T, ?, Optional<T>> reducing(BinaryOperator<T> op)
From source file:Main.java
public static void main(String... args) { Food o = Food.menu.stream()//ww w . j a va 2s. co m .collect(Collectors.reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2)).get(); System.out.println(o); }
From source file:Main.java
public static void main(String... args) { Map<Type, Optional<Food>> o = Food.menu.stream().collect(Collectors.groupingBy(Food::getType, Collectors.reducing((Food d1, Food d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2))); System.out.println(o);/* w w w. ja v a 2 s . c om*/ }
From source file:Main.java
public static void main(String... args) { Map<Type, Food> o = Food.menu.stream().collect(Collectors.groupingBy(Food::getType, Collectors.collectingAndThen( Collectors.reducing((Food d1, Food d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2), Optional::get))); System.out.println(o);/*from w w w. j a v a2s . c om*/ }
From source file:Main.java
public static void main(String... args) { Comparator<Food> dishCaloriesComparator = Comparator.comparingInt(Food::getCalories); BinaryOperator<Food> moreCaloricOf = BinaryOperator.maxBy(dishCaloriesComparator); Food o = Food.menu.stream().collect(Collectors.reducing(moreCaloricOf)).get(); System.out.println(o);/*from www.ja va 2 s . c om*/ }
From source file:Main.java
public static void main(String... args) { Stream<Food> menuStream = Food.menu.stream(); StreamForker.Results results = new StreamForker<Food>( menuStream)//from ww w. j ava 2 s.c o m .fork("shortMenu", s -> s.map( Food::getName).collect( Collectors.joining(", "))) .fork("totalCalories", s -> s.mapToInt(Food::getCalories).sum()) .fork("mostCaloricFood", s -> s.collect(Collectors .reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2)).get()) .fork("dishesByType", s -> s.collect(Collectors.groupingBy(Food::getType))).getResults(); String shortMeny = results.get("shortMenu"); int totalCalories = results.get("totalCalories"); Food mostCaloricFood = results.get("mostCaloricFood"); Map<Food.Type, List<Food>> dishesByType = results.get("dishesByType"); System.out.println("Short menu: " + shortMeny); System.out.println("Total calories: " + totalCalories); System.out.println("Most caloric dish: " + mostCaloricFood); System.out.println("Foodes by type: " + dishesByType); }
From source file:pl.prutkowski.java.playground.java8.TestCollectors.java
/** * @param args the command line arguments */// ww w .j av a 2 s . c om public static void main(String[] args) { Map<String, Integer> monthByLen = months.stream() .collect(Collectors.toMap(String::toUpperCase, m -> StringUtils.countMatches(m, "e"))); monthByLen.forEach((month, eCount) -> System.out.println(month + " -> " + eCount)); System.out.println("---------------------------------"); Map<Object, List<String>> monthByLen2 = months.stream() .collect(Collectors.groupingBy(m -> StringUtils.countMatches(m, "e"))); monthByLen2.forEach((count, groupedMonths) -> System.out.println(count + " -> " + groupedMonths)); System.out.println("---------------------------------"); Double averageLength = months.stream().collect(Collectors.averagingDouble(String::length)); System.out.println("Average length: " + averageLength); System.out.println("---------------------------------"); Double max = months.stream().collect(Collectors.summarizingDouble(String::length)).getMax(); System.out.println("Max length: " + max); System.out.println("---------------------------------"); String reduced = months.stream().collect(Collectors.reducing((m1, m2) -> (m1 + ", " + m2))).get(); System.out.println("Reduced: " + reduced); System.out.println("---------------------------------"); System.out.println(String.join(", ", months)); System.out.println("---------------------------------"); List<String> monthsWithZ = months.stream().filter(m -> m.contains("z")).collect(new ListCollector<>()); System.out.println(monthsWithZ); }
From source file:service.UtilsImpl.java
@Override public String convertPlaysToHtml(List<Play> plays) { String body = ""; Optional<String> opt = plays.stream() .map(p -> " <tr id=\"" + p.getIdPlay() + "\"" + " onclick=\"fill(" + p.getIdPlay() + ")\">\n" + " <td style =\"display:none\">" + p.getIdPlay() + "</td>" + " <td>" + p.getPlayName() + "</td>\n" + " <td>" + p.getStartDate() + "</td>\n" + " <td>" + p.getStartTime() + "</td>\n" + " <td>" + p.getEndTime() + "</td>\n" + " <td>" + p.getTicketPrice() + "</td>\n" + " </tr>\n") .collect(Collectors.reducing((i, j) -> i + j)); if (opt.get() != null) { body = " <table id=\"playTable\" class=\"table\" border=1>\n" + " <thead>\n" + " <tr>\n" + " <th>Name</th>\n" + " <th>Start Date</th>\n" + " <th>Start Time</th>\n" + " <th>End Time</th>\n" + " <th>Price</th>\n" + " </tr>\n" + " </thead>\n" + " <tbody>\n"; body += opt.get();/*from ww w. j av a2 s .co m*/ body += " </tbody>\n </table>"; } return body; }
From source file:service.UtilsImpl.java
@Override public String convertSeatsToHtml(List<Seat> places) { String body = ""; Optional<String> opt = places.stream().map(p -> " <tr id=\"" + p.getIdSeat() + "\">\n" + " <td style =\"display:none\">" + p.getIdSeat() + "</td>" + " <td>" + p.getSeatNumber() + "</td>\n" + " <td>" + p.getAvailability() + "</td>\n" + " <td>" + p.getName() + "</td>\n" + " <td><input type=\"button\" class=\"btn-primary\" onclick=\"reserve(" + p.getIdSeat() + "," + "'" + p.getAvailability() + "'" + ")\" value=\"reserve\" /></td>" + " <td><input type=\"button\" class=\"btn-primary\" onclick=\"cancel(" + p.getIdSeat() + "," + "'" + p.getAvailability() + "'" + ")\" value=\"cancel\" /></td>" + " </tr>\n").collect(Collectors.reducing((i, j) -> i + j)); if (opt.get() != null) { body = " <table class=\"table\" border=1>\n" + " <thead>\n" + " <tr>\n" + " <th>Seat Number</th>\n" + " <th>Availability</th>\n" + " <th>Name</th>\n" + " <th>Reserve Seat</th>\n" + " <th>Cancel</th>\n" + " </tr>\n" + " </thead>\n" + " <tbody>\n"; body += opt.get();//from w ww. j av a 2s. c o m body += " </tbody>\n </table>"; } return body; }
From source file:com.sillelien.dollar.api.types.DollarList.java
@NotNull @Override/* w w w. j a va2 s.com*/ public ImmutableMap<var, var> toVarMap() { AtomicInteger counter = new AtomicInteger(); return list.stream().map(var -> DollarStatic.$(String.valueOf(counter.getAndIncrement()), var)) .collect(Collectors.reducing(CollectionAware::$append)).get().toVarMap(); }