Steam的描述
Steam是中间操作
筛选和切片:
fileter--接收lambda,从流中排除某种元素
limit--截断流,使其元素不超过给定数量
skip--跳过元素,返回一个扔掉了前n个元素的流,若流中元素不足n个,则返回一个空流
排序:
sorted(Comparable)--自然排序
sorted(Comparator)--定制排序
static List<Employee> emps = Arrays.asList(
new Employee(102,"张三", 18, 3333.33),
new Employee(101,"李四", 38, 4444.44),
new Employee(104,"王五", 50, 5555.55),
new Employee(104,"王五", 50, 5555.55),
new Employee(103,"赵六", 16, 6666.66),
new Employee(105,"田七", 28, 7777.77)
);
public static void main(String[] args) {
//1.获取流
Stream<Employee> stream = emps.stream();
//2.中间操作
Stream<Employee> stream1 = stream
.filter(e->{
//System.out.println("--中间过滤操作--->"+e);
return e.getAge()>=18;
})
.distinct() //去重
//.limit(3)
//.skip(1)
//.sorted(); //默认根据内部比较器
.sorted((x,y)->Double.compare(y.getPrice(), x.getPrice()));
//3.终止行为
stream1.forEach(System.out::println);
}