1.collect(Collectors.toList())
Stream
List
listResult.forEach(i->sout(i));
输出就是 java python c++ php java
toList()把stream变成一个list 且有序可重复
2.collect(Collectors.toSet()) 无序且不重复
Stream language = Stream.of("java", "python", "C++","php","java");
Set
set.forEach(i->sout(i));
结果:python C++ java php
3.joining拼接字符串
List
//直接将输出结果拼接
sout(list.stream().collect(Collectors.joining()));
//每个输出结果之间加拼接符号“|”
sout(list.stream().collect(Collectors.joining(" | ")));
//输出结果开始头为Start--,结尾为--End,中间用拼接符号“||”
sout(list.stream().collect(Collectors.joining(" || ", "Start--", "--End")));
4.partitioningBy()
Stream l = Stream.of("java","python","C++","php","java");
Map> map = l.collect(Collectors.partitioningBy(s -> s.length() >3));
Iterator>> entry = map.entrySet().iterator();
while (entry.hasNext()){
Map.Entry> e = entry.next();
List list = e.getValue();
for (String list1 : list){
System.out.println(list1);
}
System.out.println("----------");
}
最终打印
C++
php
----------
java
python
java
----------