收集结果
收集结果(P15-19)
List<String> result = stream.collect(Collectors.toList());
Set<String> result = stream.collect(Collectors.toCollection(TreeSet::new));
//假设想要通过连接操作来收集流中的所有字符串,可以调用
String result = stream.collect(Collectors.joining());
//如果想要在元素之间增加分隔符,可以将分隔符传递给joining方法
String result = stream.collect(Collectors.joining(", "));IntSummaryStatistics summary = stream.collect(Collectors.summarizingInt(String::length));
double averageWordLength = summary.getAverage();
double maxWordLength = summary.getMax();收集到映射表中(P19-23)
Last updated