我想对一个整数列表求和。它的工作原理如下,但是语法感觉不太对。代码可以优化吗?
Map<String, Integer> integers;
integers.values().stream().mapToInt(i -> i).sum();
我想对一个整数列表求和。它的工作原理如下,但是语法感觉不太对。代码可以优化吗?
Map<String, Integer> integers;
integers.values().stream().mapToInt(i -> i).sum();
当前回答
来自文档
Reduction operations A reduction operation (also called a fold) takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation, such as finding the sum or maximum of a set of numbers, or accumulating elements into a list. The streams classes have multiple forms of general reduction operations, called reduce() and collect(), as well as multiple specialized reduction forms such as sum(), max(), or count(). Of course, such operations can be readily implemented as simple sequential loops, as in: int sum = 0; for (int x : numbers) { sum += x; } However, there are good reasons to prefer a reduce operation over a mutative accumulation such as the above. Not only is a reduction "more abstract" -- it operates on the stream as a whole rather than individual elements -- but a properly constructed reduce operation is inherently parallelizable, so long as the function(s) used to process the elements are associative and stateless. For example, given a stream of numbers for which we want to find the sum, we can write: int sum = numbers.stream().reduce(0, (x,y) -> x+y); or: int sum = numbers.stream().reduce(0, Integer::sum); These reduction operations can run safely in parallel with almost no modification: int sum = numbers.parallelStream().reduce(0, Integer::sum);
所以,对于一个地图,你会使用:
integers.values().stream().mapToInt(i -> i).reduce(0, (x,y) -> x+y);
Or:
integers.values().stream().reduce(0, Integer::sum);
其他回答
IntStream.of(1, 2, 23).sum();
IntStream.of(1, 2, 23,1, 2, 23,1, 2, 23).max().getAsInt();
可以使用collect方法添加整数列表。
List<Integer> list = Arrays.asList(2, 4, 5, 6);
int sum = list.stream().collect(Collectors.summingInt(Integer::intValue));
可以使用reduce()对整数列表求和。
int sum = integers.values().stream().reduce(0, Integer::sum);
希望这能帮助那些在清单上有物品的人。
如果您有一个对象列表,并且想要对该对象的特定字段求和,请使用下面的方法。
List<ResultSom> somList = MyUtil.getResultSom();
BigDecimal result= somList.stream().map(ResultSom::getNetto).reduce(
BigDecimal.ZERO, BigDecimal::add);
来自文档
Reduction operations A reduction operation (also called a fold) takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation, such as finding the sum or maximum of a set of numbers, or accumulating elements into a list. The streams classes have multiple forms of general reduction operations, called reduce() and collect(), as well as multiple specialized reduction forms such as sum(), max(), or count(). Of course, such operations can be readily implemented as simple sequential loops, as in: int sum = 0; for (int x : numbers) { sum += x; } However, there are good reasons to prefer a reduce operation over a mutative accumulation such as the above. Not only is a reduction "more abstract" -- it operates on the stream as a whole rather than individual elements -- but a properly constructed reduce operation is inherently parallelizable, so long as the function(s) used to process the elements are associative and stateless. For example, given a stream of numbers for which we want to find the sum, we can write: int sum = numbers.stream().reduce(0, (x,y) -> x+y); or: int sum = numbers.stream().reduce(0, Integer::sum); These reduction operations can run safely in parallel with almost no modification: int sum = numbers.parallelStream().reduce(0, Integer::sum);
所以,对于一个地图,你会使用:
integers.values().stream().mapToInt(i -> i).reduce(0, (x,y) -> x+y);
Or:
integers.values().stream().reduce(0, Integer::sum);