java中steam流的使用

java中steam流的使用,第1张

(1)数组合并 : 原始合并 定义俩个集合合并为一个集合

例子: public class 测试 {
    /**
     * 把小王合道
     * list中
     * */
    public static void main(String [] args) throws Exception {
        final ArrayList list = new ArrayList<>();
        list.add("张无忌");
        list.add("周芷若");
        list.add("赵敏");
        list.add("张强");
        list.add("张三丰");
        final ArrayList strings = new ArrayList<>();
        strings.add("小王");
        ArrayList sum = new ArrayList();
        //遍历list
        list.forEach(s -> {
            sum.add(s);
        });
        strings.forEach(s -> {
            sum.add(s);
        });
        for (Object o : sum) {
            System.out.println(o);
        }
    }
}

steam使用流合并

public class 测试 {
    /**
     * 把小王合道
     * list中
     * */
    public static void main(String [] args) throws Exception {
        final ArrayList list = new ArrayList<>();
        list.add("张无忌");
        list.add("周芷若");
        list.add("赵敏");
        list.add("张强");
        list.add("张三丰");
        final ArrayList strings = new ArrayList<>();
        strings.add("小王");
        //合并集合concat
        //list.stream()获取流对象
        Stream.concat(list.stream(),strings.stream()).forEach(System.out::println);
    }
    }

(2)steam流的长度如何获取

public class 测试 {
    /**
     * 把小王合道
     * list中
     * */
    public static void main(String [] args) throws Exception {
        final ArrayList list = new ArrayList<>();
        list.add("张无忌");
        list.add("周芷若");
        list.add("赵敏");
        list.add("张强");
        list.add("张三丰");
        final ArrayList strings = new ArrayList<>();
        strings.add("小王");
        //原始获取
        System.out.println(list.size());
        //使用流获取
        System.out.println(list.stream().count());
    }
}

如何使用steam流过滤某些字段

/**
*找出集合中叫张的人 以及他名称是3位数的 答案 张三丰 张无忌
*/
例子1 原始
   public static void main(String [] args) throws Exception {
        final ArrayList list = new ArrayList<>();
        list.add("张无忌");
        list.add("周芷若");
        list.add("赵敏");
        list.add("张强");
        list.add("张三丰");
       list.forEach(s -> {
           if (s.startsWith("张")){
               if (s.length()==3){
                   System.out.println(s);
               }
           }
       });
    }
    //------------------------过滤流写法-------------------------------------------
    public class 测试 {
    /**
     * 把小王合道
     * list中
     * */
    public static void main(String [] args) throws Exception {
        final ArrayList list = new ArrayList<>();
        list.add("张无忌");
        list.add("周芷若");
        list.add("赵敏");
        list.add("张强");
        list.add("张三丰");
        filter 过滤 已前缀过滤 s.length他的长度为3的
 list.stream().filter(s -> s.startsWith("张")).filter(s -> s.length()==3).forEach(System.out::println);
    }
    }

截取集合中的前三位数

public class 测试 {
    /**
     * 把小王合道
     * list中
     * */
    public static void main(String [] args) throws Exception {
        final ArrayList list = new ArrayList<>();
        list.add("张无忌");
        list.add("周芷若");
        list.add("赵敏");
        list.add("张强");
        list.add("张三丰");
      //原始截取方法
        for (int i = 0; i < list.size(); i++) {
          if (i<=2){
              System.out.println(list.get(i));
          }
        }
      
        //steam流的方式截取集合前三个属性
        final List collect = list.stream().limit(3).collect(Collectors.toList());
        System.out.println(collect);
    }

跳过集合中的某些属性

public class 测试 {
    /**
     * 把小王合道
     * list中
     * */
    public static void main(String [] args) throws Exception {
        final ArrayList list = new ArrayList<>();
        list.add("张无忌");
        list.add("周芷若");
        list.add("赵敏");
        list.add("张强");
        list.add("张三丰");
      //原有的
        for (int i = 0; i < list.size(); i++) {
          if (i>2){
              System.out.println(list.get(i));
          }

        }
      //steam流中的
        list.stream().skip(3).forEach(System.out::println);
    }
}

流中如何去重

public class 测试 {
    /**
     * 把小王合道
     * list中
     * */
    public static void main(String [] args) throws Exception {
        final ArrayList list = new ArrayList<>();
        list.add("张无忌");
        list.add("周芷若");
        list.add("赵敏");
        list.add("张强");
        list.add("张三丰");
        list.add("张三丰");
 /*       final HashSet strings = new HashSet<>();
        for (String s : list) {
            strings.add(s);
        }
        System.out.println("----遍历去重后的结果----");
        strings.stream().forEach(s -> {
            System.out.println(s);
        });*/
        //现在的去重如何实现
        list.stream().distinct().forEach(System.out::println);
    }
    }

steam流中如何排序

public class 测试 {
    /**
     * 把小王合道
     * list中
     * */
    public static void main(String [] args) throws Exception {
        final ArrayList list = new ArrayList<>();
        list.add(3);
        list.add(7);
        list.add(2);
        list.add(8);
        list.add(1);
     //原始排序(默认是顺序) 也可以使用冒泡排序 或者快速排序 这个是调用java中的工具类实现的排序
        /*    Collections.sort(list);*/
        //倒叙排列
        /*   Collections.reverse(list);*/
        //从大到小排列
        //Collections.sort(list,Collections.reverseOrder());
        list.stream().sorted().forEach(System.out::println);
        //System.out.println(list);
    }
}

数组如何通过流的方式转化为集合

  public static void main(String [] args) throws Exception {
        int[] a = {1, 2, 3, 4, 5};
        final List collect = Arrays.stream(a).boxed().collect(Collectors.toList());
        System.out.println(collect);
    }}

如何使用流调出数组最大值

public class 测试 {
    /**
     * 把小王合道
     * list中
     * */
    public static void main(String [] args) throws Exception {
        int[] a = {1, 2, 3, 4, 5};
        final OptionalInt max = Arrays.stream(a).max();
        System.out.println(max);
    }
}

流中至少满足一个要求''allMatch allMatch(全部满足)

public class 测试 {
    /**
     * 把小王合道
     * list中
     * */
    public static void main(String [] args) throws Exception {
        int[] a = {1, 2, 3, 4, 5};
        final boolean b = Arrays.stream(a).allMatch(c -> c > 0);
        System.out.println(b);//true
    }
}

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

原文地址: http://www.outofmemory.cn/web/992988.html

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

发表评论

登录后才能评论

评论列表(0条)

保存