List<Integer> ints = Arrays.asList(1, 2, 3, 4);
Integer sum = ints.stream()
.reduce(0, (a, b) -> a + b);
List<Integer> ints = Arrays.asList(1, 2, 3, 4);
Integer sum = ints.stream()
.reduce(0, Integer::sum);
public class MathUtils {
public static int add(int a, int b) {
return a + b;
}
}
//...
List<Integer> ints = Arrays.asList(1, 2, 3, 4);
Integer sum = ints.stream()
.reduce(0, MathUtils::add);
List<Integer> ints = Arrays.asList(1, 2, 3, 4);
Integer sum = ints.stream()
.collect(Collectors.summingInt(Integer::intValue));
List<Integer> ints = Arrays.asList(1, 2, 3, 4);
Integer sum = ints.stream()
.mapToInt(Integer::intValue)
.sum();
Integer sum = map.values()
.stream()
.mapToInt(Integer::valueOf)
.sum();
public class Item {
private int id;
private Integer price;
public Item(int id, Integer price) {
this.id = id;
this.price = price;
}
// стандартные геттеры и сеттеры
}
Item item1 = new Item(1, 5);
Item item2 = new Item(2, 10);
Item item3 = new Item(3, 15);
Item item4 = new Item(4, 20);
List<Item> items = Arrays.asList(item1, item2, item3, item4);
Integer sum = items.stream()
.map(x -> x.getPrice())
.reduce(0, ArithmeticUtils::add);
sum = items.stream()
.map(x -> x.getPrice())
.reduce(0, Integer::sum);
sum = items.stream()
.map(item -> item.getPrice())
.reduce(0, (a, b) -> a + b);
sum = items.stream()
.map(x -> x.getPrice())
.collect(Collectors.summingInt(Integer::intValue));
sum = items.stream()
.mapToInt(x -> x.getPrice())
.sum();