1 简介
流(Stream)中保存的是集合和数组数据的操作 和集合类似 但集合中存放的是数据
Stream 自己不会存数元素
Stream 不会改变原对象。相反 他们会返回一个持有结果的新Stream
Stream 操作是延迟执行。 这意味着他们会等到需要结果的时候才执行 (进行终止操作是才会执行)
使用
创建:新建一个Stream
中间:在一个过多个步骤中 将初始Stream转化到另一个Stream的中间操作
终止:使用一个终止操作来产生一个结果。该操作会强制之前的延迟操作立即执行。在这之后,该Stream就不能使用了
2 创建流
通过Collection对象的stream()或paralleIStream()方法 单线程 多线程
通过Arrays类的stream()方法
通过Stream接口的of(),iterate(),generate()方法
通过IntStream,LongStream,DoubleStream接口的of,range,rangeClosed方法
public static void main(String[] args) {
// 通过Collection对象的stream()或paralleIStream()方法
String [] strs = {"a","b","c","d"};
List<String> list = Arrays.asList(strs);
Stream<String> stream = list.stream();
stream.forEach(System.out::println);
Stream<String> parallelStream = list.parallelStream();
parallelStream.forEach(System.out::println);
//通过Arrays类的stream()方法
String [] strs2 = {"e","f","g","h"};
Arrays.stream(strs2).forEach(System.out::println);
//通过Stream接口的of(),iterate(),generate()方法
Stream.of(strs2).forEach(System.out::println);
// 迭代流
System.out.println("------------迭代流-------------");
Stream.iterate(0,x ->x+2).limit(10).forEach(System.out::println);
// 生成流
System.out.println("------------生成流-------------");
Stream.generate(() -> new Random().nextInt(100)).limit(10).forEach(System.out::println);
//通过IntStream,LongStream,DoubleStream接口的of,range,rangeClosed方法
System.out.println("---------------------------------");
IntStream.of(100,200,300).forEach(System.out::println);
IntStream.range(0,50).forEach(System.out::println);
IntStream.rangeClosed(0,50).forEach(System.out::println);
// LongStream,DoubleStream 于IntStream 一致 有时间的小伙伴自行测试
}
3 中间操作
public static void main(String[] args) {
ArrayList<Employee> list = new ArrayList<>();
list.add(new Employee("小赵",20000));
list.add(new Employee("小钱",20000));
list.add(new Employee("小孙",10000));
list.add(new Employee("小李",40000));
list.add(new Employee("小周",40000));
list.add(new Employee("小吴",30000));
list.add(new Employee("小吴",30000));
list.add(new Employee("小郑",50000));
list.add(new Employee("小王",50000));
// 中间操作 filte过滤 limit限制 skip跳过 distinct去重 sorted排序
list.stream()
.filter(e ->e.getMoney()>10000)
.limit(list.size()-2)
.skip(1)
.distinct()
.sorted((e1,e2) -> Integer.compare(e1.getMoney(),e2.getMoney()))
.forEach(System.out::println);
// 中间操作 map
list.stream()
.map(e -> e.getMoney())
.forEach(System.out::println);
}
4 终止操作
public static void main(String[] args) {
ArrayList<Employee> list = new ArrayList<>();
list.add(new Employee("小赵",20000));
list.add(new Employee("小钱",20000));
list.add(new Employee("小孙",10000));
list.add(new Employee("小李",40000));
list.add(new Employee("小周",40000));
list.add(new Employee("小吴",30000));
list.add(new Employee("小吴",30000));
list.add(new Employee("小郑",50000));
list.add(new Employee("小王",50000));
// 终止操作 foreach遍历 min()最小值 max()最大值 count()个数 reduce求和 collect收集
Optional<Employee> min = list.stream().min((e1, e2) -> Integer.compare(e1.getMoney(), e2.getMoney()));
System.out.println("-----最小值-----"+min.get());
Optional<Employee> max = list.stream().max((e1, e2) -> Integer.compare(e1.getMoney(), e2.getMoney()));
System.out.println("-----最大值-----"+max.get());
long count = list.stream().count();
System.out.println("-----个数-----"+count);
Optional<Integer> sum = list.stream().map(e -> e.getMoney()).reduce((x, y) -> x + y);
System.out.println("-----求和-----"+sum.get());
List<String> collect = list.stream().map(e -> e.getName()).collect(Collectors.toList());
System.out.println("-----收集-----"+collect);
}