Stream流

Stream流

1.什么是Stream流?
Stream流是数据渠道,用于操作集合,数组所生成的元素序列
1.Stream流自己不会存储元素
2.Stream流不会改变源对象,会返回一个持有结果的新Stream
3.Stream操作是延迟执行的
2.Stream流的作用
1.可以执行非常复杂的查找,过滤和映射数据等操作
2.可以指定对集合的操作
3.Stream的三个步骤
1.创建Stream
  集合或数组调用方法,返回与之对应的Stream流
2.中间操作
  对集合或数组的数据进行对应的处理
3.终止操作
  执行中间操作,返回结果
4.如何获取Stream流
public class Demo1 {
    public static void main(String[] args) {
        ArrayList arrayList=new ArrayList();
        Stream stream = arrayList.stream();//返回一个顺序流
        Stream stream1 = arrayList.parallelStream();//返回一个并行流
        //返回对应的数组流
        int[] arr=new int[10];
        long[] arr1=new long[20];
        double[] arr2=new double[15];
        IntStream stream2 = Arrays.stream(arr);
        DoubleStream stream3 = Arrays.stream(arr2);
        LongStream stream4 = Arrays.stream(arr1);
        //通过值创建流,返回一个元素为指定值的顺序排列的流
        Stream<Integer> integerStream = Stream.of(1, 2, 3, 45, 21);
        //返回一个无穷序列有序的Stream流
        Stream<Object> iterate = Stream.iterate(0, o -> null);
        //返回一个无穷序列无序的Stream流
        Stream<Object> generate = Stream.generate(() -> null);
    }
}
5.filter(过滤,得到自己所需要的元素)
public class Demo2 {
    public static void main(String[] args) {
        List<Employee> list = Arrays.asList(
                new Employee(102, "李四", 59, 6666.66),
                new Employee(101, "张三", 18, 9999.99),
                new Employee(103, "王五", 28, 3333.33),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(105, "田七", 38, 5555.55)
        );
        Stream<Employee> stream = list.stream();
        //获取年龄大于30的人
        Stream<Employee> employeeStream = stream.filter(new Predicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getAge() > 30;
            }
        });
       employeeStream.forEach(System.out::println);
        System.out.println("=======================");
       //获取姓张的员工
        Stream<Employee> stream1 = list.stream();
        stream1.filter(new Predicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getName().startsWith("张");
            }
        }).forEach(System.out::println);
    }
}

//输出结果
Employee [id=102, name=李四, age=59, salary=6666.66, status=null]
Employee [id=105, name=田七, age=38, salary=5555.55, status=null]
=======================
Employee [id=101, name=张三, age=18, salary=9999.99, status=null]
6.distinct(去重),必须重写存储的元素的hashcode和equals方法
public class Deno3 {
    public static void main(String[] args) {
        List<Employee> list = Arrays.asList(
                new Employee(102, "李四", 59, 6666.66),
                new Employee(101, "张三", 18, 9999.99),
                new Employee(103, "王五", 28, 3333.33),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(105, "田七", 38, 5555.55)
        );
    //切记,执行去重方法时必须重写元素的hashcode和equals方法,否则不会有效果
       /*
       //一步一步来
       Stream<Employee> stream = list.stream();
        Stream<Employee> distinctStream = stream.distinct();
        distinctStream.forEach(System.out::println);*/
       //链式编程
        list.stream().distinct().forEach(System.out::println);
    }
}
//输出结果
Employee [id=102, name=李四, age=59, salary=6666.66, status=null]
Employee [id=101, name=张三, age=18, salary=9999.99, status=null]
Employee [id=103, name=王五, age=28, salary=3333.33, status=null]
Employee [id=104, name=赵六, age=8, salary=7777.77, status=null]
Employee [id=105, name=田七, age=38, salary=5555.55, status=null]
7.limit(截断,类似于数据库中的limit)
public class Demo4 {
    public static void main(String[] args) {
        List<Employee> list = Arrays.asList(
                new Employee(102, "李四", 59, 6666.66),
                new Employee(101, "张三", 18, 9999.99),
                new Employee(103, "王五", 28, 3333.33),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(105, "田七", 38, 5555.55)
        );
        Stream<Employee> stream = list.stream();
        Stream<Employee> employeeStream = stream.filter(new Predicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getAge() > 30;
            }
        });
        //首先过滤出年龄大于30的员工,然后截取第一个
        Stream<Employee> limit = employeeStream.limit(1);
        limit.forEach(System.out::println);
    }
}
//输出结果
Employee [id=102, name=李四, age=59, salary=6666.66, status=null]
8.skip(跳过,返回一个丢弃前n个元素的流,若流中元素少于n个,则返回一个空流)
public class Demo5 {
    public static void main(String[] args) {
        List<Employee> list = Arrays.asList(
                new Employee(102, "李四", 59, 6666.66),
                new Employee(101, "张三", 18, 9999.99),
                new Employee(103, "王五", 28, 3333.33),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(105, "田七", 38, 5555.55)
        );
        Stream<Employee> stream = list.stream();
        Stream<Employee> distinctStream = stream.distinct();//首先对Stream流进行去重
        //跳过了两个元素,分别是李四和张三
        Stream<Employee> skipStream = distinctStream.skip(2);
        skipStream.forEach(System.out::println);
        //输出结果
        Employee [id=103, name=王五, age=28, salary=3333.33, status=null]
        Employee [id=104, name=赵六, age=8, salary=7777.77, status=null]
        Employee [id=105, name=田七, age=38, salary=5555.55, status=null]

    }
}
9.map方法
public class Demo6 {
    public static void main(String[] args) {
        List<Employee> list = Arrays.asList(
                new Employee(102, "李四", 59, 6666.66),
                new Employee(101, "张三", 18, 9999.99),
                new Employee(103, "王五", 28, 3333.33),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(105, "田七", 38, 5555.55)
        );
        Stream<Employee> stream = list.stream();
        Stream<Object> objectStream = stream.map(new Function<Employee, Object>() {
            @Override
            public Object apply(Employee employee) {
                return employee.getName();
            }
        });
        objectStream.distinct().forEach(System.out::println);
    }
}
//输出结果
李四
张三
王五
赵六
田七
public class Demo7 {
    public static void main(String[] args) {
        //求集合中每个元素的平方
        List<Integer> list = Arrays.asList(1, 2, 3, 4);
        Stream<Integer> stream = list.stream();
        Stream<Object> objectStream = stream.map(new Function<Integer, Object>() {
            @Override
            public Object apply(Integer integer) {
                return Math.pow(integer, 2);
            }
        });
        objectStream.forEach(System.out::println);
    }
}
//输出结果
1.0
4.0
9.0
16.0
10.sorted方法(排序,自然排序或比较器排序)
public class Demo9 {
    public static void main(String[] args) {
        List<Employee> list = Arrays.asList(
                new Employee(102, "李四", 59, 6666.66),
                new Employee(101, "张三", 18, 9999.99),
                new Employee(103, "王五", 28, 3333.33),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(105, "田七", 38, 5555.55)
        );
        Stream<Employee> stream = list.stream();
        stream.sorted((o1, o2) -> o1.getAge()-o2.getAge()).distinct().forEach(System.out::println);
    }
}
//输出结果
Employee [id=104, name=赵六, age=8, salary=7777.77, status=null]
Employee [id=101, name=张三, age=18, salary=9999.99, status=null]
Employee [id=103, name=王五, age=28, salary=3333.33, status=null]
Employee [id=105, name=田七, age=38, salary=5555.55, status=null]
Employee [id=102, name=李四, age=59, salary=6666.66, status=null]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值