Last active
March 31, 2017 18:42
-
-
Save carlosmaniero/bfc72a04ba242d59056f92324b9d6727 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
class Fair { | |
private final int quantity; | |
Fair(int quantity) { | |
this.quantity = quantity; | |
} | |
int getQuantity() { | |
return quantity; | |
} | |
} | |
public class ReduceFair { | |
public static void main(String args[]) { | |
Map<String, Integer> products = new HashMap<>(); | |
products.put("Banana", 7); | |
products.put("Apple", 2); | |
products.put("Kiwi", 1); | |
int totalInTheFair = products.keySet().stream() | |
.reduce( | |
new Fair(0), | |
(accFair, productName) -> new Fair(accFair.getQuantity() + products.get(productName)), | |
(fair, fair2) -> new Fair(fair.getQuantity() + fair2.getQuantity())) | |
.getQuantity(); | |
System.out.println(totalInTheFair); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment