Java--Stream的中间 *** 作

Java--Stream的中间 *** 作,第1张

1: 筛选与切片
    /**
     * 筛选与切片
     */
    @Test
    public void test1() {
        List employeeList = new ArrayList<>();
        employeeList.add(new Employee(1, "张三", 18, 9999.99));
        employeeList.add(new Employee(2, "李四", 59, 6666.66));
        employeeList.add(new Employee(3, "王五", 28, 3333.33));
        employeeList.add(new Employee(4, "赵六", 8, 7777.77));
        employeeList.add(new Employee(5, "田七", 38, 5555.55));
        employeeList.add(new Employee(6, "马云", 35, 9876.23));
        employeeList.add(new Employee(7, "比尔盖茨", 67, 5637.78));
        employeeList.add(new Employee(8, "乔布斯", 59, 9387.33));
        employeeList.add(new Employee(9, "扎克伯格", 35, 6786.23));
        employeeList.add(new Employee(8, "乔布斯", 59, 9387.33));

        System.out.println("---------------------------------");

        Stream stream = employeeList.stream();
        //查询:筛选工资大于7000的员工,filter(e -> e.getSalary() > 7000)
        stream.filter(e -> e.getSalary() > 7000).forEach(System.out::println);

        System.out.println("---------------------------------");

        //切片:筛选工资大于7000的员工,并取前3个,limit(3)
        Stream stream1 = employeeList.stream();
        stream1.filter(e -> e.getSalary() > 7000).limit(3).forEach(System.out::println);

        System.out.println("---------------------------------");

        //跳过前三个,skip(3)
        Stream stream2 = employeeList.stream();
        stream2.skip(3).forEach(System.out::println);

        System.out.println("---------------------------------");

        //筛选重复数据,通过流的distinct()方法,根据生成元素的hashCode()和equals()去重
        Stream stream3 = employeeList.stream();
        stream3.distinct().forEach(System.out::println);
    }

运行结果:
  ---------------------------------
  Employee{id=1, name='张三', age=18, salary=9999.99}
  Employee{id=4, name='赵六', age=8, salary=7777.77}
  Employee{id=6, name='马云', age=35, salary=9876.23}
  Employee{id=8, name='乔布斯', age=59, salary=9387.33}
  Employee{id=8, name='乔布斯', age=59, salary=9387.33}
  ---------------------------------
  Employee{id=1, name='张三', age=18, salary=9999.99}
  Employee{id=4, name='赵六', age=8, salary=7777.77}
  Employee{id=6, name='马云', age=35, salary=9876.23}
  ---------------------------------
  Employee{id=4, name='赵六', age=8, salary=7777.77}
  Employee{id=5, name='田七', age=38, salary=5555.55}
  Employee{id=6, name='马云', age=35, salary=9876.23}
  Employee{id=7, name='比尔盖茨', age=67, salary=5637.78}
  Employee{id=8, name='乔布斯', age=59, salary=9387.33}
  Employee{id=9, name='扎克伯格', age=35, salary=6786.23}
  Employee{id=8, name='乔布斯', age=59, salary=9387.33}
  ---------------------------------
  Employee{id=1, name='张三', age=18, salary=9999.99}
  Employee{id=2, name='李四', age=59, salary=6666.66}
  Employee{id=3, name='王五', age=28, salary=3333.33}
  Employee{id=4, name='赵六', age=8, salary=7777.77}
  Employee{id=5, name='田七', age=38, salary=5555.55}
  Employee{id=6, name='马云', age=35, salary=9876.23}
  Employee{id=7, name='比尔盖茨', age=67, salary=5637.78}
  Employee{id=8, name='乔布斯', age=59, salary=9387.33}
  Employee{id=9, name='扎克伯格', age=35, salary=6786.23}  
2: 映射
    /**
     * 映射
     */
    @Test
    public void test2() {
        //map(Function f): 接受一个函数作为参数,将元素转换成其他形式或提取信息,该函数会被应用到每个元素上,并将其映射成一个新的元素
        //1:将集合中的字母转为大写
        List list = Arrays.asList("aa", "bb", "cc");
//        list.stream().map(s -> s.toUpperCase()).forEach(System.out::println);
        list.stream().map(String::toUpperCase).forEach(System.out::println);

        System.out.println("---------------------------------");

        Stream stringStream = list.stream().map(String::toUpperCase);
        //将Stream转换成List集合
        List list1 = stringStream.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        System.out.println(list1);

        System.out.println("---------------------------------");
        //2:获取员工姓名长度大于3的员工
        List employeeList = new ArrayList<>();
        employeeList.add(new Employee(1, "张三", 18, 9999.99));
        employeeList.add(new Employee(2, "李四", 59, 6666.66));
        employeeList.add(new Employee(3, "王五", 28, 3333.33));
        employeeList.add(new Employee(4, "赵六", 8, 7777.77));
        employeeList.add(new Employee(5, "田七", 38, 5555.55));
        employeeList.add(new Employee(6, "马云", 35, 9876.23));
        employeeList.add(new Employee(7, "比尔盖茨", 67, 5637.78));
        employeeList.add(new Employee(8, "乔布斯", 59, 9387.33));
        employeeList.add(new Employee(9, "扎克伯格", 35, 6786.23));
        //获取到所有员工的姓名
        Stream nameStream = employeeList.stream().map(Employee::getName);
        //获取到所有员工姓名长度大于3的员工
        nameStream.filter(name -> name.length() > 3).forEach(System.out::println);


        System.out.println("---------------------------------");

        //flatMap(Function f): 接受一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流
        //1:循环集合中的各个元素
        List list2 = Arrays.asList("aa", "bb", "cc", "dd", "ee");
        //循环集合中字符串的各个元素
        Stream stringStream1 = list2.stream().flatMap(s -> Arrays.stream(s.split("")));
        stringStream1.forEach(System.out::println);

    }
运行结果: 
  AA
  BB
  CC
  ---------------------------------
  [AA, BB, CC]
  ---------------------------------
  比尔盖茨
  扎克伯格
  ---------------------------------
  a
  a
  b
  b
  c
  c
  d
  d
  e
  e
3: 排序
    /**
     * 排序
     */
    @Test
    public void test3() {
        //1:sorted():产生一个新流,其中按照自然顺序排列
        List list = Arrays.asList(12, 23, 34, -12, 67, 89, 1, 24);
        list.stream().sorted().forEach(System.out::println);

        System.out.println("---------------------------------");

        //对于对对象排序是,对象类必须实现Comparable接口,并且实现compareTo方法或使用定制排序
        //2:sorted(Comparator com):产生一个新流,其中按比较器顺序排序
        List employeeList = new ArrayList<>();
        employeeList.add(new Employee(1, "张三", 28, 9999.99));
        employeeList.add(new Employee(2, "李四", 59, 6666.66));
        employeeList.add(new Employee(3, "王五", 28, 3333.33));
        employeeList.add(new Employee(4, "赵六", 8, 7777.77));
        employeeList.add(new Employee(5, "田七", 38, 5555.55));
        employeeList.add(new Employee(6, "马云", 35, 9876.23));
        employeeList.add(new Employee(7, "比尔盖茨", 67, 5637.78));
        employeeList.add(new Employee(8, "乔布斯", 59, 9387.33));
        employeeList.add(new Employee(9, "扎克伯格", 35, 6786.23));
        //根据员工年龄升序(从小到达)排序
        //方式一:
        employeeList.stream().sorted((e1, e2) -> Integer.compare(e1.getAge(), e2.getAge())).forEach(System.out::println);
        //方式二:employeeList.stream().sorted(Comparator.comparingInt(Employee::getAge)).forEach(System.out::println);
        System.out.println("---------------------------------");
        //当年龄相同时,按照工资升序排序
        //方式一:
        employeeList.stream().sorted((e1, e2) ->{
            int compare = Integer.compare(e1.getAge(), e2.getAge());
            if (compare == 0) {
                compare = Double.compare(e1.getSalary(), e2.getSalary());
            }
            return compare;
        }).forEach(System.out::println);
        //方式二:employeeList.stream().sorted(Comparator.comparingInt(Employee::getAge).thenComparingDouble(Employee::getSalary)).forEach(System.out::println);
    }

运行结果:
  -12
  1
  12
  23
  24
  34
  67
  89
  ---------------------------------
  Employee{id=4, name='赵六', age=8, salary=7777.77}
  Employee{id=1, name='张三', age=28, salary=9999.99}
  Employee{id=3, name='王五', age=28, salary=3333.33}
  Employee{id=6, name='马云', age=35, salary=9876.23}
  Employee{id=9, name='扎克伯格', age=35, salary=6786.23}
  Employee{id=5, name='田七', age=38, salary=5555.55}
  Employee{id=2, name='李四', age=59, salary=6666.66}
  Employee{id=8, name='乔布斯', age=59, salary=9387.33}
  Employee{id=7, name='比尔盖茨', age=67, salary=5637.78}
  ---------------------------------
  Employee{id=4, name='赵六', age=8, salary=7777.77}
  Employee{id=3, name='王五', age=28, salary=3333.33}
  Employee{id=1, name='张三', age=28, salary=9999.99}
  Employee{id=9, name='扎克伯格', age=35, salary=6786.23}
  Employee{id=6, name='马云', age=35, salary=9876.23}
  Employee{id=5, name='田七', age=38, salary=5555.55}
  Employee{id=2, name='李四', age=59, salary=6666.66}
  Employee{id=8, name='乔布斯', age=59, salary=9387.33}
  Employee{id=7, name='比尔盖茨', age=67, salary=5637.78}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/922936.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存