java8的Stream的常用方法

参考: https://www.jianshu.com/p/007c76a417ab
https://www.runoob.com/java/java8-streams.html
https://blog.csdn.net/y_k_y/article/details/84633001
https://blog.csdn.net/yy1098029419/article/details/89452380
https://zhuanlan.zhihu.com/p/265884828

java 8 中新增了两个流方法,分别是Stream() 和 parallelStream()(并行流)
和iterator(迭代器)的区别:
Iterator,只能显式地一个一个遍历元素并对其执行某些操作,
而Stream只要给出对其元素的操作,Stream会吟诗的在内部进行遍历,做出相应的操作;

流操作的常用方法:
1,获取流

 // 1 Collection 获取流
   List<String> list = new ArrayList<>();
   Stream<String> stream1 = list.stream();
   // Map获取流
   Map<String, String> map = new HashMap<>();
   Stream<String> keyStream = map.keySet().stream();
   Stream<String> valueStream = map.values().stream();
   Stream<Map.Entry<String, String>> entryStream = map.entrySet().stream();
 // 根据数组获取流
   String[] array = { "张无忌", "张翠山", "张三丰", "张一元" };
   Stream<String> stream = Stream.of(array);

2 流的操作
1).单元素操作:

List<String> names = Arrays.asList("1张三","2赵四","3张四","4李四","5王五","6赵柳","7张六","8王少","9赵四","10张仁","11李星");
     List<String> list = names.stream()
        .filter(x -> x.endsWith("四"))  // 过滤
        .skip(1)   // 跳过n元素,配合limit(n)可实现分页
        .limit(7)  //获取n个元素
        .distinct() // 去重
        .map(x -> x + "123") //接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。
        .distinct()   //去重
        .sorted()  //排序 升序
        // .sorted(Comparator.reverseOrder()) //排序 降序
        .collect(Collectors.toList());
         //    .count(); // 返回流中元素的总个数
        //     .max();   // 返回流中元素最大值
        //     .min() ;  // 返回流中元素最小值
        //     .findFirst(); // 返回流中第一个元素
       //      .findAny(); //返回流中的任意元素
System.out.println(list);

2)对象操作
参考: https://blog.csdn.net/xy3233/article/details/117931652
https://blog.csdn.net/y_k_y/article/details/84633001

@Data
class Student {
    
    
    public Student(String name, Integer age) {
    
    
        this.age = age;
        this.name = name;
    }
    private String name;
    private Integer age;
}


@Test
public void demo2(){
    
    
       List<Student> list = new ArrayList<>();
       list.add(new Student("张三", 3));
       list.add(new Student("王二", 2));
       list.add(new Student("麻子", 5));
       list.add(new Student("李四", 4));
       list.add(new Student("李明", 6));
       list.add(new Student("李明", 1));
       list.add(new Student("李明", 1));
       List<Student> orderList = list.stream()
               .filter(x->x.name.startsWith("李"))  // 过滤
               .distinct()  // 去重 属性都一样 才去重
              // .sorted(Comparator.comparing(Student::getAge)) //按年纪升序排序
                .sorted(Comparator.comparing(Student::getAge).reversed()) //降序 或者使用 Collections.reverse(orderList)
               .collect(Collectors.toList());
    System.out.println(orderList);

       // 按姓名去重
    ArrayList<Student> studentArrayList  = list.stream().
            collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new));
    System.out.println(studentArrayList);

    // 年龄的集合
    List<Integer> collect = studentArrayList.stream()
            .map(x -> x.age) // 或
            .collect(Collectors.toList());
    System.out.println(collect);
// 最大年龄
    Student student = studentArrayList.stream().max(Comparator.comparing(Student::getAge)).get();
    System.out.println(student);
    //平均年龄
    Double averageAge = list.stream().collect(Collectors.averagingInt(Student::getAge));
    System.out.println(averageAge);
    // 按年龄分组
    Map<Integer, List<Student>> collect1 = list.stream().collect(Collectors.groupingBy(Student::getAge));
    System.out.println(collect1);

    // 按年龄 姓名 成为map
    Map<Integer, String> collect2 = list.stream()
            .distinct()
            .collect(Collectors.toMap(Student::getAge, Student::getName));
    System.out.println(collect2);

    // 年龄  对象   成map
    Map<Integer, Student> collect3 = list.stream()
            .distinct()
            .collect(Collectors.toMap(Student::getAge, student1 -> student1));
    System.out.println(collect3);
}

    // 使用map 创建一个全新的list
        List<Student> collect4 = list.stream()
                .map(x -> x.age > 3 ? new Student("张三", x.age) : new Student("李四", x.age))
                .collect(Collectors.toList());
        System.out.println(collect4);

        // 使用map 创建一个全新的list
        List<Student> collect5 = list.stream()
                .map(x ->{
    
    
                    if( x.age > 3){
    
    
                        return new Student("张三", x.age);
                    }else{
    
    
                        return new Student("李四", x.age);
                    }
                }).collect(Collectors.toList());
        System.out.println(collect5);


猜你喜欢

转载自blog.csdn.net/xy3233/article/details/121121005
今日推荐