Java Stream API - count items of a nested list

前端 未结 3 672
情歌与酒
情歌与酒 2020-12-05 23:40

Let\'s assume that we have a list of countries: List and each country has a reference to a list of its regions: List

3条回答
  •  自闭症患者
    2020-12-06 00:32

    You could even use flatMap() like:

    countries.stream().map(Country::getRegions).flatMap(List::stream).count();
    
    where,
    
    map(Country::getRegions) = returns a Stream>
    flatMap(List::stream) = returns a Stream
    

提交回复
热议问题