List of usage examples for java.util.stream Collectors groupingBy
public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier)
From source file:Main.java
public static void main(String... args) { Map<Type, List<Food>> o = Food.menu.stream().collect(Collectors.groupingBy(Food::getType)); System.out.println(o);//from w ww . j av a 2 s. c o m }
From source file:Main.java
public static void main(String... args) { Map<Boolean, Map<Type, List<Food>>> o = Food.menu.stream() .collect(Collectors.partitioningBy(Food::isVegetarian, Collectors.groupingBy(Food::getType))); System.out.println(o);//from w w w. j a v a 2 s . c o m }
From source file:Main.java
public static void main(String[] args) { List<Trade> trades = TradeUtil.createTrades(); Map<String, List<Trade>> issuers = trades.stream().collect(Collectors.groupingBy(t -> t.getIssuer())); System.out.println("Grouped List: " + issuers); }
From source file:Main.java
public static void main(String[] args) throws Exception { List<Person> persons = Arrays.asList(new Person("Max", 18), new Person("Peter", 23), new Person("Pamela", 23), new Person("David", 12)); Map<Integer, List<Person>> personsByAge = persons.stream().collect(Collectors.groupingBy(p -> p.age)); personsByAge.forEach((age, p) -> System.out.format("age %s: %s\n", age, p)); }
From source file:Main.java
public static void main(String[] args) { List<Person> roster = createRoster(); System.out.println("Members by gender:"); Map<Person.Sex, List<Person>> byGender = roster.stream().collect(Collectors.groupingBy(Person::getGender)); List<Map.Entry<Person.Sex, List<Person>>> byGenderList = new ArrayList<>(byGender.entrySet()); byGenderList.stream().forEach(e -> { System.out.println("Gender: " + e.getKey()); e.getValue().stream().map(Person::getName).forEach(f -> System.out.println(f)); });//from w ww.ja v a 2 s . c o m }
From source file:Main.java
public static void main(String... args) { List<Transaction> transactions = Arrays.asList(new Transaction(Currency.EUR, 1500.0), new Transaction(Currency.USD, 2300.0), new Transaction(Currency.GBP, 9900.0), new Transaction(Currency.EUR, 1100.0), new Transaction(Currency.JPY, 7800.0), new Transaction(Currency.CHF, 6700.0), new Transaction(Currency.EUR, 5600.0), new Transaction(Currency.USD, 4500.0), new Transaction(Currency.CHF, 3400.0), new Transaction(Currency.GBP, 3200.0), new Transaction(Currency.USD, 4600.0), new Transaction(Currency.JPY, 5700.0), new Transaction(Currency.EUR, 6800.0)); Map<Currency, List<Transaction>> transactionsByCurrencies = transactions.stream() .collect(Collectors.groupingBy(Transaction::getCurrency)); System.out.println(transactionsByCurrencies); }
From source file:com.doctor.java8.GroupingTheData.java
public static void main(String[] args) { List<Person> persons = Arrays.asList(new Person("doctor", "man", "address-1"), new Person("doctor who ", "man", "address-1"), new Person("doctor me", "woman", "address-2")); Map<String, List<Person>> map = persons.stream().collect(Collectors.groupingBy(Person::getSex)); System.out.println(map);//from ww w . j a v a 2 s . c om Map<String, Map<String, List<Person>>> map2 = persons.stream() .collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getAddress))); System.out.println(map2); }
From source file:Main.java
public static void main(String... args) { Stream<Food> menuStream = Food.menu.stream(); StreamForker.Results results = new StreamForker<Food>( menuStream)// ww w . j a v a 2 s . co 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:entity.service.FamilyentryFacadeREST.java
@GET @Path("rid/{id}") @Produces({ "application/json" }) public Response findAllWithEntries(@PathParam("id") Integer id) throws JsonProcessingException { em.flush();/*from w ww . ja v a 2 s . c o m*/ List<Entry> familyentries = (List) em .createQuery("SELECT e FROM Entry e WHERE e.key.raceId = :raceid AND e.familyentry IS NOT NULL") .setParameter("raceid", id).getResultList(); Map<String, List<Entry>> entriesByFamilyentry = familyentries.stream() .collect(Collectors.groupingBy(e -> e.getFamilyentry().getName())); String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(entriesByFamilyentry); return Response.ok(json).build(); }
From source file:be.boyenvaesen.scheduling.HumiditySchedules.java
@Scheduled(fixedRate = 2000) public void updateIntervalDatabases() { log.info("Starting database cleanup"); Calendar c = Calendar.getInstance(); Calendar calForCalculations = Calendar.getInstance(); //Clean records dating till : c.add(Calendar.HOUR, -5);/*from ww w . j a v a2 s.c o m*/ Date now = new Date(); List<Humidity> allHumidities = service.getBetweenDates(c.getTime(), now); List<HumidityByMinute> humiditiesByMinute = service.getBetweenDatesByInterval(HumidityByMinute.class, c.getTime(), now); List<HumidityByHour> humidityByHour = service.getBetweenDatesByInterval(HumidityByHour.class, c.getTime(), now); //Group humidities by minute Map<Date, List<Humidity>> map = allHumidities.stream().collect(Collectors.groupingBy((t) -> { calForCalculations.setTime(t.getMeasured()); calForCalculations.set(Calendar.SECOND, 0); calForCalculations.set(Calendar.MILLISECOND, 0); return calForCalculations.getTime(); })); //CALCULATE MINUTES map.forEach((t, u) -> { HumidityByMinute toCalc = humiditiesByMinute.stream().filter((q) -> { return q.getAtTime().compareTo(t) == 0; }).findAny().orElse(new HumidityByMinute(t)); float average = (float) u.stream().mapToDouble(Humidity::getPercentage).average().getAsDouble(); toCalc.setAveragePercentage(average); service.saveByInterval(toCalc); }); List<HumidityByMinute> newByMinute = service.getBetweenDatesByInterval(HumidityByMinute.class, c.getTime(), now); //Group HumidityByMinute By hour Map<Date, List<HumidityByMinute>> mapByHour = newByMinute.stream().collect(Collectors.groupingBy((t) -> { calForCalculations.setTime(t.getAtTime()); calForCalculations.set(Calendar.MINUTE, 0); return calForCalculations.getTime(); })); //CALCULATE HOURS mapByHour.forEach((t, u) -> { HumidityByHour toCalc = humidityByHour.stream().filter((q) -> { return q.getAtTime().compareTo(t) == 0; }).findAny().orElse(new HumidityByHour(t)); float average = (float) u.stream().mapToDouble(HumidityByMinute::getAveragePercentage).average() .getAsDouble(); toCalc.setAveragePercentage(average); service.saveByInterval(toCalc); }); log.info("finished cleaning database"); }