Java 8 Streams Java Java API
The static method, Collectors.summingLong() returns a Collector which uses a provided mapper function to convert each input element of type T to primitive long, and returns the sum of the resulting long values.
<T> Collector<T,?,Long> summingLong(ToLongFunction<? super T> mapper)
Examples
package com.logicbig.example.collectors;
import java.math.BigDecimal; import java.util.stream.Collectors; import java.util.stream.Stream;
public class SummingLongExample { public static void main (String[] args) {
Stream<BigDecimal> s = Stream.iterate( BigDecimal.ONE, bigDecimal -> bigDecimal.add(BigDecimal.ONE)) .limit(10) .peek(System.out::println);
long l = s.collect(Collectors.summingLong( (BigDecimal::longValue))); System.out.println("sum: " + l); } }
Output1 2 3 4 5 6 7 8 9 10 sum: 55
Original Post
|