Skip to content

Instantly share code, notes, and snippets.

@carlosmaniero
Last active March 31, 2017 18:42
Show Gist options
  • Save carlosmaniero/bfc72a04ba242d59056f92324b9d6727 to your computer and use it in GitHub Desktop.
Save carlosmaniero/bfc72a04ba242d59056f92324b9d6727 to your computer and use it in GitHub Desktop.
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