import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test2
{
public static void main(String[] args) {
List<Stu> list=new ArrayList();
Stu stu1=new Stu(15, "张飞");
Stu stu2=new Stu(19, "关于");
Stu stu3=new Stu(22,"刘备");
list.add(stu1);
list.add(stu2);
list.add(stu3);
Collections.shuffle(list);
/* 操作步骤:集合->生成流->中间操作->终端操作
* eg: list.stream.limit(2).collect(Collectors.toList());
*
*
* 1.中间操作
* - 操作流
* map
* filter
* limit
* sort
* reduce
* max min count distinct
* foreach
* match
*
* 2.终端操作
*-对操作后的流进行组织后返回。
* .collect( Collectors.doSomeThing);
* summarizing:getMax ,min count sum avarage
* grouping by
* joining
* partitioningBy
*
*
*/
//对流的操作 :过滤,映射,排序,
Map<String,String> map=new HashMap<>();
System.out.println("=========list===========");
System.out.println(list);
System.out.println("=====filter(Predicate):过滤集合 ==========");
List<Stu> filterList=list.stream().filter(u->u.age>=19&&"关于".equals(u.name)).collect(Collectors.toList());
System.out.println("======map(Function) 根据输入(类型A)获取输出(类型B) ==========");
List<Stu> mapList=list.stream().map(a->new Stu(a.age+2, a.name)).collect(Collectors.toList());
List<Integer> mapList2=list.stream().map(a->a.age+2).collect(Collectors.toList());
System.out.println(mapList);
System.out.println("========limit ========");
List<Stu> limitList=list.stream().limit(2).collect(Collectors.toList());
System.out.println(limitList);
System.out.println("========sorted 排序========");
List<Stu> sortList = list.stream().sorted(Comparator.comparing(a->a.age)).collect(Collectors.toList());
List<Stu> reverseSortList = list.stream().sorted(Comparator.comparing(a->a.age, Comparator.reverseOrder())).collect(Collectors.toList());
System.out.println(reverseSortList);
System.out.println("===reduce 迭代进行归并 ============");
int value = Stream.of(1, 2, 3, 4, 5)
.reduce(((item1, item2) -> item1 + item2)).get();
System.out.println("===============foreach 遍历Consumer==========");
list.stream().forEach(a->a.age=a.age+3);
System.out.println(list);
System.out.println("=========================match匹配=========");
System.out.println(list.stream().anyMatch(a->a.age<12));
//=======================================================Collectors===================================================================================
System.out.println("========average 平均数=======");
System.out.println(""+list.stream().collect(Collectors.averagingInt(a->a.age)));
System.out.println("=========group by 分组======");
Map<Object, List<Stu>>groupMap=list.stream().collect(Collectors.groupingBy(a->a.age));
System.out.println(groupMap);
System.out.println("====group by counting 分组统计==========");
Map<Object, Long>groupCountMap=list.stream().collect(Collectors.groupingBy(a->a.age,Collectors.counting()));
System.out.println(groupCountMap);
System.out.println("==============summarizing 统计===");
System.out.println(list.stream().collect(Collectors.summarizingInt(a->a.age)).getMax());
System.out.println("=========mapping T->R =====");
List<Stu> mapping=list.stream().collect(Collectors.mapping(a->new Stu(a.age+2, a.name),Collectors.toList() ));
//避免空指针异常 Potional.ofNullable().map()....orElse();
String s=Optional.ofNullable(new Stu(12,"22")).map(Stu::getName).orElse(null);
}
}
class Stu{
int age;
String name;
public Stu(int age, String name) {
super();
this.age = age;
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Stu [age=" + age + ", name=" + name + "]";
}
}