Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. class Fair {
  5. private final int quantity;
  6.  
  7. Fair(int quantity) {
  8. this.quantity = quantity;
  9. }
  10.  
  11. int getQuantity() {
  12. return quantity;
  13. }
  14. }
  15.  
  16. public class ReduceFair {
  17. public static void main(String args[]) {
  18. Map<String, Integer> products = new HashMap<>();
  19. products.put("Banana", 7);
  20. products.put("Apple", 2);
  21. products.put("Kiwi", 1);
  22.  
  23. int totalInTheFair = products.keySet().stream()
  24. .reduce(new Fair(0), (accFair, productName) -> {
  25. return new Fair(accFair.getQuantity() + products.get(productName));
  26. }, (fair, fair2) -> new Fair(fair.getQuantity() + fair2.getQuantity()))
  27. .getQuantity();
  28.  
  29. System.out.println(totalInTheFair);
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement