List<String> strList1 = Arrays.asList("aaa", "bbb", "ccc");
List<String> strList2 = Arrays.asList("ddd", "eee", "fff");
List<List<String>> list = Arrays.asList(strList1,strList2);
int noOfElements = (int) list.stream().flatMap(i->i.stream()).count();
System.out.println(noOfElements);
Run Code Online (Sandbox Code Playgroud)
我想计算个人数量Strings
(如上面的代码片段所示).在展平列表后我能够这样做,但我想在不使用的情况下做同样的事情flatMap()
.必须有一些方法来计算.提前致谢.
tob*_*s_k 10
您可以map
将个人列表发送给他们size
并计算sum
:
int noOfElements = list.stream().mapToInt(List::size).sum();
Run Code Online (Sandbox Code Playgroud)
或者使用reduce
替代,如建议在评论由@MarkusBenko:
int noOfElements = list.stream().reduce(0, (i, l) -> i + l.size(), (i, j) -> i + j)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1515 次 |
最近记录: |