Java Stream API - count items of a nested list

2019-01-25 08:56发布

问题:

Let's assume that we have a list of countries: List<Country> and each country has a reference to a list of its regions: List<Region> (e.g. states in the case of the USA). Something like this:

USA
  Alabama
  Alaska
  Arizona
  ...

Germany
  Baden-Württemberg
  Bavaria
  Brandenburg
  ...

In "plain-old" Java we can count all regions e.g. this way:

List<Country> countries = ...
int regionsCount = 0;

for (Country country : countries) {
    if (country.getRegions() != null) {
        regionsCount += country.getRegions().size();
    }
}

Is it possible to achieve the same goal with Java 8 Stream API? I thought about something similar to this, but I don't know how to count items of nested lists using count() method of stream API:

countries.stream().filter(country -> country.getRegions() != null).???

回答1:

You could use map() to get a Stream of region lists and then mapToInt to get the number of regions for each country. After that use sum() to get the sum of all the values in the IntStream:

countries.stream().map(Country::getRegions) // now it's a stream of regions
                  .filter(rs -> rs != null) // remove regions lists that are null
                  .mapToInt(List::size) // stream of list sizes
                  .sum();

Note: The benefit of using getRegions before filtering is that you don't need to call getRegions more than once.



回答2:

You may map each country to number of regions and then reduce result using sum:

countries.stream()
  .map(c -> c.getRegions() == null ? 0 : c.getRegions().size())
  .reduce(0, Integer::sum);


回答3:

You could even use flatMap() like:

countries.stream().map(Country::getRegions).flatMap(List::stream).count();

where,

map(Country::getRegions) = returns a Stream<List<Regions>>
flatMap(List::stream) = returns a Stream<Regions>