package s0224Stream.reduce测试; |
import java.util.HashSet; |
import java.util.Set; |
public class Main { |
public static void main(String[] args) { |
// 这种向集合中添加元素的方式学习一下 |
Set<String> set= new HashSet<String>(){{ add( "张三" );add( "王五" );add( "李四" );}}; |
String t=set.stream().reduce( "" , (s,e)->s+= " " +e); |
//reduce(identity,accumulator) identity是字符串起始字串,后面是累加操作 |
System.out.println(t); |
|
String t2=set.stream().reduce( "q" , (s,e)->s+=e); |
//reduce(identity,accumulator) identity是字符串起始字串,后面是累加操作 |
System.out.println(t2); |
} |
} |