Facebook
From Jittery Shama, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 196
  1.  // 11. Zwróć firmę która dokonała zakupów na największą kwotę
  2.     public static void exercise11X(List<Company> companies){
  3.         companies
  4.                 .stream()
  5.                 .collect(Collectors.groupingBy(Company::getName, Collectors.mapping(c -> c.getPurchaseList().stream().mapToDouble(p -> p.getQuantity() * p.getProduct().getPrice()).sum() , Collectors.toList())));
  6.  
  7.  
  8.          companies
  9.                 .stream()
  10.                 .collect(Collectors.toMap(Company::getName, c -> c.getPurchaseList().stream().mapToDouble(p -> p.getQuantity() * p.getProduct().getPrice()).sum(), (c1,c2) -> c1+c2))
  11.                 .entrySet()
  12.                 .stream()
  13.                 .max(Comparator.comparingDouble(Entry::getValue));
  14.  
  15.     }
  16.  
  17.     public static Optional<Company> exercise11(List<Company> companies){
  18.  
  19.          return companies
  20.                  .stream()
  21.                  .max(Comparator.comparingDouble(c -> c.getPurchaseList().stream().mapToDouble(p -> p.getQuantity() * p.getProduct().getPrice()).sum()));
  22.  
  23.     }
  24.  
  25.  
  26.     // 14. Wypisz firmy które 15 stycznia 2018 kupiły "Network Switch"
  27.     public static void exercise14(List<Company> companies){
  28.         companies
  29.                 .stream()
  30.                 .filter(c -> c.getPurchaseList().stream().map(p -> p.getPurchaseDate()).anyMatch(d -> d.isEqual(LocalDate.of(2018, 1, 15))))
  31.                 .filter(c -> c.getPurchaseList().stream().map(p -> p.getProduct().getName()).anyMatch(n -> n.equalsIgnoreCase("network switch")))
  32.                 .forEach(c -> System.out.println(c.getName() +  " " + c.getCityHeadquarters()));
  33.     }