stream流案例


    public static void main(String[] args) {

        List<Person> personList = new ArrayList();
        personList.add(new Person("Tom", 8900, "male", "New York"));
        personList.add(new Person("Jack", 7000, "male", "Washington"));
        personList.add(new Person("Lily", 7800, "female", "Washington"));
        personList.add(new Person("Anni", 8200, "female", "New York"));
        personList.add(new Person("Owen", 9500, "male", "New York"));
        personList.add(new Person("Alisa", 7900, "female", "New York"));

        List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
//        // 遍历输出符合条件的元素
        list.stream().filter(x -> x > 6).forEach(System.out::print);
//
//        // 匹配第一个
//        Optional findFirst = list.stream().filter(x -> x > 6).findFirst();
//        // 匹配任意(适用于并行流)
//        Optional findAny = list.parallelStream().filter(x -> x > 6).findAny();
//        // 是否包含符合特定条件的元素
//        boolean anyMatch = list.stream().anyMatch(x -> x > 6);
//
//
        System.out.println("匹配第一个值:" + findFirst.get());
        System.out.println("匹配任意一个值:" + findAny.get());
        System.out.println("是否存在大于6的值:" + anyMatch);

        /*
         * 筛选
         */
//        list.stream().filter(a->a>7).forEach(System.out::println);
        Collections.sort(list);
//        list.stream().filter(a -> a > 5).forEach(System.out::println);


//        筛选员工中工资高于 8000 的人,并形成新的集合。形成新集合依赖(收集)

//        List<String> collect = personList.stream().filter(person -> person.getSalary() >= 8000).map(person -> person.getName()).collect(Collectors.toList());
//        System.out.println(collect);

        // 自然排序
//        Optional max = list.stream().max(Integer::compareTo);
//        System.out.println(max.get());
//        // 自定义排序
//        Optional<Integer> max1 = list.stream().max(new Comparator<Integer>() {
//            @Override
//            public int compare(Integer o1, Integer o2) {
//                return o1.compareTo(o2);
//            }
//        });
//        System.out.println(max1.get());

        //获取员工工资最高的人
//        Optional<Person> max = personList.stream().max(Comparator.comparingInt(Person::getSalary));
//        System.out.println("员工工资最大值:" + max.get().getSalary());

        //案例四:计算集合中大于 6 的元素的个数。
//        long count = list.stream().filter(a -> a > 6).count();
//        System.out.println(count);

        // 映射(map/flatMap)
//        案例一:英文字符串数组的元素全部改为大写。整数数组每个元素+3。
//        String[] strArr = { "abcd", "bcdd", "defde", "fTr" };
//        List<String> collect = Arrays.asList(strArr).stream().map(String::toUpperCase).collect(Collectors.toList());
//        System.out.println("collect:"+collect);
//        List strList = Arrays.stream(strArr).map(String::toUpperCase).collect(Collectors.toList());
//        List<Integer> intList = Arrays.asList(1, 3, 5, 7, 9, 11);
//        List intListNew = intList.stream().map(x -> x + 3).collect(Collectors.toList());
//        System.out.println("每个元素大写:" + strList);
//        System.out.println("每个元素+3:" + intListNew);


//        案例二:将员工的薪资全部增加 1000。
//        List<Integer> personSalaryAdd = personList.stream().map(p -> p.getSalary() + 1000).collect(Collectors.toList());;
//        System.out.println(personSalaryAdd);

//        案例三:将两个字符数组合并成一个新的字符数组。
        List<String> lists = Arrays.asList("m,k,l,a", "1,3,5,7");
         lists.stream().flatMap(s -> {
            String[] split = s.split(",");
            Stream s2 = Arrays.stream(split);
            return s2;
        }).collect(Collectors.toList());

//        System.out.println(lists);

        //归约(reduce)  归约,也称缩减,顾名思义,是把一个流缩减成一个值,能实现对集合求和、求乘积和求最值操作
        // 求和方式1
//        Optional sum = list.stream().reduce((x, y) -> x + y);
//        System.out.println(sum.get());
        // 求和方式2
//        Optional sum2 = list.stream().reduce(Integer::sum);
//        System.out.println(sum.get());
        // 求和方式3
//        Integer sum3 = list.stream().reduce(, Integer::sum);

        // 求乘积
//        Optional product = list.stream().reduce((x, y) -> x * y);
//        System.out.println(product.get());

        // 求最大值方式1
//        Optional max = list.stream().reduce((x, y) -> x > y ? x : y);
//        System.out.println(max.get());

        // 求最大值写法2
//        Integer max2 = list.stream().reduce(1, Integer::max);
//        System.out.println(max2);

//        案例二:求所有员工的工资之和和最高工资。
//        Optional<Integer> reduce = personList.stream().map(Person::getSalary).reduce((x, y) -> x + y);
//        System.out.println(reduce.get());
//        Optional<Person> max1 = personList.stream().max(Comparator.comparingInt(Person::getSalary));
//        System.out.println(max1.get().getSalary());
//        Optional<Person> max1 = personList.stream().map(Person::getSalary).max();
//        System.out.println(max1.get());

        //收集(collect) 可以说是内容最繁多、功能最丰富的部分了。从字面上去理解,就是把一个流收集起来,最终可以是收集成一个值也可以收集成一个新的集合。
//        归集(toList/toSet/toMap) 因为流不存储数据,那么在流中的数据完成处理后,需要将流中的数据重新归集到新的集合里。、和比较常用,另外还有、等复杂一些的用法

        List listNew = list.stream().filter(x -> x % 2 ==0 ).collect(Collectors.toList());

        Set set = list.stream().filter(x -> x % 2 ==0 ).collect(Collectors.toSet());

        Map<String, Person> collect = personList.stream().filter(x -> x.getSalary() % 2 == 0).collect(Collectors.toMap(Person::getName, p -> p));

        for (String s : collect.keySet()) {
            System.out.println(collect.get(s));
        }

//        for (Integer integer : list) {
//            System.out.println(integer+" ");
//        }

//        for (Object o : listNew) {
//            System.out.print(o+" ");
//        }
//        Iterator iterator = set.iterator();
//        while (iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
    }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值