Java Collectors.summingInt: Summing Integer Streams
Learn how to use Java Collectors.summingInt to sum integer values from streams, including custom object properties, with practical code examples and edge-case handling.

When you need to sum a series of int values from a Java stream, Collectors.summingInt is the collector designed for that task. It returns a Collector<T, ?, Integer> that reduces the stream to a single Integer sum and fits naturally into the Stream.collect pipeline. The method signature is public static <T> Collector<T, ?, Integer> summingInt(ToIntFunction<? super T> mapper), so it accepts a function that extracts an int from each stream element. This makes it useful for summing a numeric property from a collection of objects, not just raw integers.
How Collectors.summingInt Works
Collectors.summingInt uses a mutable accumulator to track a running total. For each element, it applies the mapper to produce an int and adds that value to the accumulator. Because the accumulator stores primitive int values, the per-element reduction does not box every intermediate sum; only the final Integer result is boxed.
During parallel execution, Stream.collect creates separate accumulators for different parts of the stream and then uses the collector's combiner to merge partial results. For summingInt, the combiner simply adds the partial sums.
Basic Usage with a Stream of Integers
If you already have a Stream<Integer>, you can use summingInt with a method reference that unboxes the value. For example:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream()
.collect(Collectors.summingInt(Integer::intValue));Here, Integer::intValue is a ToIntFunction<Integer> that returns the primitive int value of each Integer. The collector then sums those values and returns an Integer that is auto-unboxed to int in the assignment. This works for any Stream<Integer>, but if you have an IntStream directly, you would typically call sum() on it instead.
Summing a Property from a Custom Object
A more common use case is summing a numeric field from a list of domain objects. Consider a Person class with an age field:
class Person {
private String name;
private Integer age;
private String city;
// constructor, getters, etc.
public Integer getAge() { return age; }
public String getCity() { return city; }
}You can sum all ages with:
List<Person> people = getPeople();
int totalAge = people.stream()
.collect(Collectors.summingInt(Person::getAge));Person::getAge is compatible with ToIntFunction<Person>; Java unboxes the returned Integer when the method reference is adapted to the collector's mapper. This is concise and avoids writing an explicit loop. It also composes well with other collectors, such as groupingBy, where you might want the sum of a property per group.
Using summingInt with mapToInt and sum()
Java provides an alternative for summing integers: mapToInt(...).sum(). For example:
int totalAge = people.stream()
.mapToInt(Person::getAge)
.sum();Both approaches produce the same result. The key difference is that mapToInt returns an IntStream, which has a built-in sum() method that operates on primitive values. This is slightly more direct and often more readable when you only need the sum. However, summingInt becomes valuable when you are already working with Collectors—for instance, as a downstream collector in a groupingBy operation:
Map<String, Integer> totalAgeByCity = people.stream()
.collect(Collectors.groupingBy(Person::getCity,
Collectors.summingInt(Person::getAge)));In such cases, you cannot use mapToInt().sum() directly because you need a Collector. The choice depends on whether you are building a collector pipeline or simply computing a standalone sum.
Handling Null Values and Empty Streams
Collectors.summingInt returns 0 for an empty stream. This is convenient because it avoids a null check for the collection-level result.
However, the mapper must not be applied to a null Integer value. If an object property is boxed and can be null, a NullPointerException occurs when the mapper unboxes it. For example, you can provide a default value:
int totalAge = people.stream()
.collect(Collectors.summingInt(p -> p.getAge() != null ? p.getAge() : 0));This lambda checks for a null age before unboxing and uses 0 as the default. If age were a primitive int, this check would be unnecessary. The general rule is to make sure the mapped value is non-null before using summingInt.
Performance and Overhead Considerations
Both summingInt and mapToInt().sum() avoid per-element boxing in the reduction because they work with primitive int values. The real overhead to watch for is having a Stream<Integer> instead of an IntStream; a Stream<Integer> contains boxed values before the collector runs. If you already have an IntStream, use sum() directly. If you have a Stream<Integer>, summingInt gives you a clean way to unbox and sum without writing a manual loop.
One subtlety: summingInt returns an Integer, not an int. If you are summing values that might exceed Integer.MAX_VALUE, the sum will overflow silently. In such cases, consider using Collectors.summingLong or switching to a long accumulator.
Common Pitfalls and Edge Cases
First, summingInt does not filter null elements. If the stream contains a null Person, Person::getAge cannot be invoked, so the stream operation throws a NullPointerException. Filter nulls before collecting:
int totalAge = people.stream()
.filter(p -> p != null)
.collect(Collectors.summingInt(Person::getAge));Second, summingInt is designed for int values only; use summingLong or summingDouble for long or double properties respectively. Third, an infinite stream would never terminate, so ensure the source is finite.
When to Choose summingInt Over Other Summation Approaches
Deciding between summingInt, mapToInt().sum(), and a manual loop depends on the context. Use summingInt when you are already using the Collectors API, such as in a groupingBy or partitioningBy downstream, or when you need to combine multiple collectors. Use mapToInt().sum() for a simple, standalone sum where you want to avoid the collector abstraction. A manual loop is rarely necessary, but it might be clearer for very small collections or when you need to perform additional operations during iteration. The key is to choose the approach that matches the surrounding code style and the need for composability. If you are summing a property from a list of objects and want a readable one-liner, summingInt is often the best fit.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.