How to sum a list of integers with java streams?
This will work, but the i -> i
is doing some automatic unboxing which is why it "feels" strange. mapToInt
converts the stream to an IntStream
"of primitive int-valued elements". Either of the following will work and better explain what the compiler is doing under the hood with your original syntax:
integers.values().stream().mapToInt(i -> i.intValue()).sum();
integers.values().stream().mapToInt(Integer::intValue).sum();
Java 8 Streams - Why can't I sum a Stream of Integers?
Calling stream()
on a List
will get you a general-purpose Stream
, which can handle any reference type, not just numeric types. It doesn't make sense to include a sum
method on Stream
, because it doesn't make sense to sum URL
s, Class
es, Thread
s, or any other non-numeric reference type.
You could sum the elements by calling reduce
:
numbers.stream().reduce(0, (a, b) -> a + b)
But that would involve a lot of unboxing and boxing. It would be best to sum them as you have it, by converting it to an IntStream
, which operates on int
s, and calling sum()
(or summaryStatistics()
, which includes count, average, max, and min along with sum).
You could even use IntStream.of
and avoid boxing the values even once.
IntStream.of(1, 2, 3).sum()
How to sum every elements in a list of a list using stream?
You can do it like this:
List<List<Integer>> cases = Arrays.asList(Arrays.asList(1, 1, 2), Arrays.asList(3, 3, 2), Arrays.asList(4, 5, 1));
List<Integer> result = cases.stream()
.map(list -> list.stream().mapToInt(Integer::intValue).sum())
.collect(Collectors.toList());
System.out.println(result); // print [4, 8, 10]
Sum only first few values of the list using Java8
Use limit()
as shown below
int sum = list.stream().sorted().limit(3).mapToInt(Integer::intValue).sum();
How to sum the values in Listint[] using Java 8
You want to flatMap
to an IntStream
. After that, taking the sum is easy.
int sum = counts.stream()
.flatMapToInt(IntStream::of)
.sum();
Calculating List of cumulative sums from List of integers with Java streams
Try this.
int[] a = {4, 5, 8, -11, 9, 5, -7, 4, 6, -6, -8, -11, 80, -32, -56, -15, 5, -49};
Arrays.parallelPrefix(a, (x, y) -> x + y);
System.out.println(Arrays.toString(a));
output:
[4, 9, 17, 6, 15, 20, 13, 17, 23, 17, 9, -2, 78, 46, -10, -25, -20, -69]
Java map / collect for Stream to sum List of Lists
Because you have a List<List<Integer>>
, it seems that a vertical sum should produce a List<Integer>
, as each column will have its own sum. Assuming every row in the List<List<Integer>>
contains the same number of elements, it would be a good idea to first find the transpose of the List<List<Integer>>
, sum every row, and collect it back into a List<Integer>
. You can do it using the following:
List<List<Integer>> list = Arrays.asList(
Arrays.asList(1, 2, 3, 4),
Arrays.asList(5, 6, 7, 8)
);
List<Integer> sumList = IntStream.range(0, list.get(0).size())
.mapToObj(i -> list.stream()
.mapToInt(l -> l.get(i))
.sum())
.collect(Collectors.toList());
System.out.println(sumList);
Output:
[6, 8, 10, 12]
Related Topics
Can't Find a Way to Color the Mandelbrot-Set the Way I'm Aiming For
How to Calculate the Number of Rows (And Columns in Each Row) a Text Takes in a Jtextarea
List Versus Arraylist as Reference Type
How to Populate a Listview in Javafx Using Custom Objects
Accessing a Variable from Another Class
What Is a Difference Between <? Super E> and <? Extends E>
Get List of JSON Objects with Spring Resttemplate
How Do Synchronized Static Methods Work in Java and How to Use It for Loading Hibernate Entities
Killing Thread After Some Specified Time Limit in Java
How to Use Java.String.Format in Scala
Why Isn't There a Java.Lang.Array Class? If a Java Array Is an Object, Shouldn't It Extend Object