/**
* 收集器
*/
@Test
public void test4(){
List collect = Stream.of("年后", "看懂", "看到").collect(Collectors.toList());
Set collect2 = Stream.of("年后", "看懂", "看到").collect(Collectors.toSet());
//返回不可修改的集合 如果修改将抛出异常UnsupportedOperationException
Set collect3 = Stream.of("年后", "看懂", "看到").collect(Collectors.toUnmodifiableSet());
List collect4 = Stream.of("年后", "看懂", "看到").collect(Collectors.toUnmodifiableList());
//使用非接口List,Set类型
TreeSet collect1 = Stream.of("年后", "看懂", "看到").collect(Collectors.toCollection(TreeSet::new));
LinkedList collect5 = Stream.of("年后", "看懂", "看到").collect(Collectors.toCollection(LinkedList::new));
//比较大小获取最大值
String s = Stream.of("年后", "看懂", "看到").collect(Collectors.maxBy((x, y) -> x.compareTo(y))).get();
System.out.println(s);
String s1 = Stream.of("年后", "看懂", "看到").collect(Collectors.minBy((x, y) -> x.compareTo(y))).get();
System.out.println(s1);
//数据统计
IntSummaryStatistics collect6 = Stream.of("1", "2", "3").map(x -> Integer.valueOf(x)).collect(Collectors.summarizingInt(x -> x.intValue()));
System.out.println(String.format("sum:%d,avg:%f,max:%d,min:%d,count:%d", collect6.getSum(),collect6.getAverage(),collect6.getMax(),collect6.getMin(),collect6.getCount()));
//数据分块
Map> collect7 = Stream.of(new Man(), new Man(), new Girl(), new Girl(), new Girl()).collect(Collectors.partitioningBy(x -> "女人".equals(x.getName())));
System.out.println(collect7);
//数据分组
Map> collect8 = Stream.of(new Man(), new Man(), new Girl(), new Girl(), new Girl()).collect(Collectors.groupingBy(Person::getName));
System.out.println(collect8);
//拼接字符串
String collect9 = Stream.of(new Man(), new Man(), new Girl(), new Girl(), new Girl()).map(x -> x.getName()).collect(Collectors.joining());
System.out.println(collect9);
//添加分割符
String collect10= Stream.of(new Man(), new Man(), new Girl(), new Girl(), new Girl()).map(x -> x.getName()).collect(Collectors.joining(","));
System.out.println(collect10);
//添加前后缀
String collect11= Stream.of(new Man(), new Man(), new Girl(), new Girl(), new Girl()).map(x -> x.getName()).collect(Collectors.joining(",","{","}"));
System.out.println(collect11);
List list = new ArrayList<>(5);
for (int i = 0; i < 5; i++) {
Chicken chicken = new Chicken();
chicken.setName(String.valueOf(ThreadLocalRandom.current().nextInt(3)));
List lt = new LinkedList<>();
for (int k = 0; k < ThreadLocalRandom.current().nextInt(); k++) {
lt.add(new Chicken());
}
chicken.setDescendants(lt);
list.add(chicken);
}
System.out.println(list);
//分组并获取该组一共有多少个相同名称的鸡
Map collect12 = list.stream().collect(Collectors.groupingBy(Chicken::getName, Collectors.counting()));
System.out.println(collect12);
//分组获取每组子节点最多的对象的option
Map> collect13 = list.stream().collect(Collectors.groupingBy(Chicken::getName, Collectors.maxBy((x, y) -> Integer.valueOf(x.getDescendants().size()).compareTo(y.getDescendants().size()))));
System.out.println(collect13);
//根据上面的option获取每组子节点最多的对象
List collect15 = collect13.entrySet().stream().map(x -> x.getValue().get()).collect(Collectors.toList());
System.out.println(collect15);
//映射 根据名称分组并统计所有相同名称下的所有字节点总数
Map collect14 = list.stream().collect(Collectors.groupingBy(Chicken::getName, Collectors.mapping(Chicken::getDescendants, Collectors.summingInt(x -> x.size()))));
System.out.println(collect14);
}
看懂
年后
sum:6,avg:2.000000,max:3,min:1,count:3
{false=[com.demo.Man@18a70f16, com.demo.Man@62e136d3], true=[com.demo.Girl@c8e4bb0, com.demo.Girl@6279cee3, com.demo.Girl@4206a205]}
{人=[com.demo.Man@4d826d77, com.demo.Man@61009542], 女人=[com.demo.Girl@77e9807f, com.demo.Girl@448ff1a8, com.demo.Girl@1a38c59b]}
人人女人女人女人
人,人,女人,女人,女人
{人,人,女人,女人,女人}
[Chicken{name='0', descendants=[Chicken{name='null', descendants=null}, Chicken{name='null', descendants=null}, Chicken{name='null', descendants=null}]}, Chicken{name='2', descendants=[]}, Chicken{name='0', descendants=[Chicken{name='null', descendants=null}]}, Chicken{name='0', descendants=[]}, Chicken{name='0', descendants=[]}]
{0=4, 2=1}
{0=Optional[Chicken{name='0', descendants=[Chicken{name='null', descendants=null}, Chicken{name='null', descendants=null}, Chicken{name='null', descendants=null}]}], 2=Optional[Chicken{name='2', descendants=[]}]}
[Chicken{name='0', descendants=[Chicken{name='null', descendants=null}, Chicken{name='null', descendants=null}, Chicken{name='null', descendants=null}]}, Chicken{name='2', descendants=[]}]
{0=4, 2=0}