Stream流常用方法

1.filter过滤

    public static void filter(){
        List<User> list = users();
        List<User> newlist = list.stream().filter(user -> user.getAge() > 20)
                .collect(Collectors.toList());
        for (User user : newlist) {
            System.out.println(user.getName()+" --> "+ user.getAge());
        }
    }

2.distinct去重

    public static void distinct(){
        List<User> list = users();
        List<User> newlist = list.stream().distinct().collect(Collectors.toList());
        for (User user : newlist) {
            System.out.println(user.getName()+" --> "+ user.getAge());
        }
    }

3.sorted排序

   public static void sorted(){
        List<User> list = users();
        List<User> newlist = list.stream()
                .sorted(Comparator.comparingInt(User::getAge))
                .collect(Collectors.toList());
        for (User user : newlist) {
            System.out.println(user.getName()+" --> "+ user.getAge());
        }
    }

4.limit返回前n个元素

    public static void limit(){
        List<User> list = users();
        List<User> newlist = list.stream()
                .sorted(Comparator.comparingInt(User::getAge))
                .limit(2)
                .collect(Collectors.toList());
        for (User user : newlist) {
            System.out.println(user.getName()+" --> "+ user.getAge());
        }
    }

5.skip去除前n个元素

    public static void skip(){
        List<User> list = users();
        List<User> newlist = list.stream()
                .sorted(Comparator.comparingInt(User::getAge))
                .skip(2)
                .collect(Collectors.toList());
        for (User user : newlist) {
            System.out.println(user.getName()+" --> "+ user.getAge());
        }
    }

6.map(T->R)

  public static void map(){
        List<User> list = users();
        List<String> newlist = list.stream()
                .map(user ->
                    user.getName()+"-"+user.getAge()+"-"+user.getAddress()).distinct().collect(Collectors.toList());
        for (String add : newlist) {
            System.out.println(add);
        }
    }

7.flatMap(T -> Stream)

    public static void flatmap(){
        List<String> flatmap = new ArrayList<>();
        flatmap.add("常宣灵,常昊灵");
        flatmap.add("孟婆,判官红,判官蓝");
        /*
            这里原集合中的数据由逗号分割,使用split进行拆分后,得到的是Stream<String[]>,
            字符串数组组成的流,要使用flatMap的Arrays::stream
            将Stream<String[]>转为Stream<String>,然后把流相连接
        */
        flatmap = flatmap.stream()
                .map(s -> s.split(","))
                .flatMap(Arrays::stream)
                .collect(Collectors.toList());
        for (String name : flatmap) {
            System.out.println(name);
        }
    }

8.allMatch(T->boolean)检测是否全部满足参数行为

public static void allMatch(){
        List<User> list = users();
        boolean flag = list.stream()
                .allMatch(user -> user.getAge() >= 16);
        System.out.println(flag);
    }

9.anyMatch(T->boolean)检测是否有任意元素满足给定的条件

 public static void anyMatch(){
        List<User> list = users();
        boolean flag = list.stream()
                .anyMatch(user -> user.getSex() == 1);
        System.out.println(flag);
    }

10.noneMatchT->boolean)流中是否有元素匹配给定的 T -> boolean条件

  public static void noneMatch(){
        List<User> list = users();
        boolean flag = list.stream()
                .noneMatch(user -> user.getAddress().contains("万毒窟"));
        System.out.println(flag);
    }

11.findFirst( ):找到第一个元素

   public static void findfirst(){
        List<User> list = users();
        Optional<User> optionalUser = list.stream()
                .sorted(Comparator.comparingInt(User::getAge))
                .findFirst();
        System.out.println(optionalUser.toString());
    }   

12.findAny( ):找到任意一个元素

  public static void findAny(){
        List<User> list = users();
        Optional<User> optionalUser = list.stream()
                .findAny();
        System.out.println(optionalUser.toString());
    }

13.计算总数

  public static void count(){
        List<User> list = users();
        long count = list.stream().count();
        System.out.println(count);
    }

14.最大值最小值

    public static void max_min(){
        List<User> list = users();
        Optional<User> max = list.stream()
                .collect(
                        Collectors.maxBy(
                                Comparator.comparing(User::getAge)
                        )
                );
        Optional<User> min = list.stream()
                .collect(
                        Collectors.minBy(
                                Comparator.comparing(User::getAge)
                        )
                );
        System.out.println("max--> " + max+"  min--> "+ min);
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值