stream流的常用方法

做个记录
参考博客:https://blog.csdn.net/weixin_44702189/article/details/126135202

package com.test.java8.stream;

import com.test.java8.pojo.User;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author清梦
 * @site www.xiaomage.com
 * @company xxx公司
 * @create 2023-06-01 16:44
 */
public class StreamTest {

    //https://blog.csdn.net/weixin_52317961/article/details/128117727
    //注:是一个终结方法,遍历之后就不能继续调用Stream流中的其他方法
    public static void foreachTest(List<User> list){
        //使用Stream流的方法forEach对stream流中的数据遍历
        list.stream().forEach((User user)->{
            System.out.println("有参数类型:"+ user.getId() + "-" + user.getName() + "-" + user.getAge());
        });
        //可以省略参数类型,如
        list.stream().forEach(user->{
            System.out.println("无参数类型:" + user.getId() + "-" + user.getName() + "-" + user.getAge());
        });
    }

    public static void filterTest(List<User> list){
        List<User> collect = list.stream().filter(user -> {
            return user.getAge() > 30;
        }).collect(Collectors.toList());
        collect.stream().forEach(user->{
            System.out.println("满足条件的有:" + user.getId() + "-" + user.getName() + "-" + user.getAge());
        });

    }


    //distinct()不对对象列表进行去重的直接实现
    public static void distinctTest(List<User> list){
        List<User> collect = list.stream().distinct().collect(Collectors.toList());
        collect.stream().forEach(user-> {
            System.out.println("去重后:" + user.getId() + "-" + user.getName() + "-" + user.getAge());
        });
    }

    public static void distinctTestInteger(List<Integer> list){
        List<Integer> collect = list.stream().distinct().collect(Collectors.toList());
        collect.stream().forEach(id-> {
            System.out.println("去重后:" + id);
        });
    }

    public static void limitTest(List<User> list){
        List<User> collect = list.stream().limit(2).collect(Collectors.toList());
        collect.stream().forEach(user-> {
            System.out.println("截取两个元素:" + user.getId() + "-" + user.getName() + "-" + user.getAge());
        });
    }

    public static void skipTest(List<User> list){
        List<User> collect = list.stream().skip(3).collect(Collectors.toList());
        collect.stream().forEach(user-> {
            System.out.println("跳过3个个元素:" + user.getId() + "-" + user.getName() + "-" + user.getAge());
        });
    }

    public static void sortTest(List<User> list){
        List<User> collect = list.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList());
        collect.stream().forEach(user-> {
            System.out.println("按年龄从大到小排序:" + user.getId() + "-" + user.getName() + "-" + user.getAge());
        });
    }

    public static void MinMaxTest(List<User> list){
        User min = list.stream().min(Comparator.comparing(User::getAge)).get();
        System.out.println("年龄最小的人:" + min.getId() + "-" + min.getName() + "-" + min.getAge());
        User max = list.stream().max(Comparator.comparing(User::getAge)).get();
        System.out.println("年龄最大的人:" + max.getId() + "-" + max.getName() + "-" + max.getAge());
    }

    public static void SumTest(List<User> list){
        int sum = list.stream().mapToInt(User::getAge).sum();
        System.out.println("年龄总和:"+sum);
    }

    public static void objectToMap(List<User> list){
        list.remove(3);
        list.remove(3);
        Map<String, User> collect = list.stream().collect(Collectors.toMap(User::getName, user -> user));
        collect.forEach((name,user)->{
            System.out.println("姓名:"+name+",用户:" + user.getId() + "-" + user.getName() + "-" + user.getAge() );
        });
    }

    public static void findTest(List<User> list){
        Optional<User> first = list.stream().findFirst();
        if (first.isPresent()){
            User user = first.get();
            System.out.println("第一个用户:" + user.getId() + "-" + user.getName() + "-" + user.getAge() );
        }
    }

    //stream和parallelStream的简单区分:
    //stream是顺序流,由主线程按顺序对流执行操作,
    //parallelStream是并行流,内部以多线程并行执行的方式对流进行操作,但前提是流中的数据处理没有顺序要求。
    public static void findAny(List<User> list){
        Optional<User> any = list.parallelStream().findAny();
        if (any.isPresent()){
            User user = any.get();
            System.out.println("任意用户:" + user.getId() + "-" + user.getName() + "-" + user.getAge() );
        }
    }

    public static void anyMatchTest(List<User> list){
        boolean b = list.stream().anyMatch(user -> {
            return user.getAge() > 30;
        });
        System.out.println("是否包含年龄大于30岁的用户:" + b);
    }

    public static void countTest(List<User> list){
        long count = list.stream().filter(user -> {
            return user.getAge() > 23;
        }).count();
        System.out.println("年龄大于23的用户数量:" + count);
    }

    public static void reduceTest(List<Integer> list){
        //求和方式1
        Optional<Integer> reduce = list.stream().reduce((x, y) -> x + y);
        if (reduce.isPresent()){
            System.out.println("求和1结果:" + reduce.get());
        }
        //求和方式2
        Optional<Integer> reduce1 = list.stream().reduce(Integer::sum);
        if (reduce.isPresent()){
            System.out.println("求和2结果:" + reduce1.get());
        }

        //求和方式3
        Integer reduce2 = list.stream().reduce(0, Integer::sum);
        if (reduce.isPresent()){
            System.out.println("求和3结果:" + reduce1.get());
        }
    }

    public static void averageTest(List<User> list){
        Double collect = list.stream().collect(Collectors.averagingInt(User::getAge));
        System.out.println("用户平均年龄:" + collect);

    }

    public static void main(String[] args) {
        List<User> list = new ArrayList<>();
        User u1 = new User(1,"lucy",26);
        User u2 = new User(2,"李明",21);
        User u3 = new User(3,"嘎子",35);
        User u4 = new User(2,"李明",21);
        User u5 = new User(4,"李明",25);
        list.add(u1);
        list.add(u2);
        list.add(u3);
        list.add(u4);
        list.add(u5);
        List<Integer> ids = Arrays.asList(1,2,2,3,4);
        /*foreachTest(list);
        filterTest(list);
        distinctTest(list);
        distinctTestInteger(ids);

        limitTest(list);
        skipTest(list);
        sortTest(list);
        MinMaxTest(list);
        sumTest(list);
        objectToMap(list);
        findTest(list);
        findAny(list);
        anyMatchTest(list);*/
        countTest(list);
        reduceTest(ids);
        averageTest(list);

    }

}

你可能感兴趣的:(java,servlet,jvm,stream)