How to sum the values in List<int[]> using Java 8

BreenDeen :

I want to find the sum of the List<int[]> using Java 8. Here is my attempt.

int sum = counts.stream().flatMap(i -> Stream.of(i).mapToInt(m)).sum();

However, I get the error cannot convert to Stream<Object> to <unknown>.

Ward :

You want to flatMap to an IntStream. After that, taking the sum is easy.

int sum = counts.stream()
        .flatMapToInt(IntStream::of)
        .sum();

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=428321&siteId=1