Loading

Streams 过滤器

Streams 过滤器

演示使用流的filter(),collect(),findAny()和orElse()

使用list过滤

    public List<Student> getStudents(){
        List<Student> result = new ArrayList<>();
        result.add(new Student(1,"小易","21"));
        result.add(new Student(2,"小二","23"));
        result.add(new Student(4,"小思","24"));
        result.add(new Student(3,"小三","24"));
        return result;
    }

    @Test
    public void StreamsFilterTest() {
        List<Student> studentList = getFilterOutput(getStudents(), "小三");
        System.out.println("把小三给我过滤掉====>"+studentList);
    }

    public List<Student> getFilterOutput(List<Student> liens ,String filter){
        List<Student> result = new ArrayList<>();
        for (Student student : liens) {
            //过滤你想过滤掉的学生
            if (!filter.equals(student.getName())){
                result.add(student);
            }
        }
        return result;
    }

使用stream.filter()

stream.filter()来过滤List,并collect()以流转换为List

    @Test
    public void StreamsFilterTest() {
        List<Student> studentList = getStudents().stream()
                .filter(student -> !"小三".equals(student.getName()))
                .collect(Collectors.toList());
        studentList.forEach(student-> System.out.println("把小三给我剔除了---->"+student));
    }

Streams filter(), findAny() and orElse()

1.8之前

    public List<Student> getStudents(){
        List<Student> result = new ArrayList<>();
        result.add(new Student(1,"小易","21"));
        result.add(new Student(2,"小二","23"));
        result.add(new Student(4,"小思","24"));
        result.add(new Student(3,"小三","24"));
        return result;
    }

    @Test
    public void StreamsFilterTest() {
        Student studentByName = getStudentByName(getStudents(), "小三");
        System.out.println(studentByName);
    }

    public Student getStudentByName(List<Student> students,String name){
        Student student = null;
        for (Student studentTemp : students) {
            if (name.equals(studentTemp.getName())){
                student = studentTemp;
            }
        }
        return student;
    }

1.8之后可以用

    public List<Student> getStudents(){
        List<Student> result = new ArrayList<>();
        result.add(new Student(1,"小易","21"));
        result.add(new Student(2,"小二","23"));
        result.add(new Student(4,"小思","24"));
        result.add(new Student(3,"小三","24"));
        return result;
    }

    @Test
    public void StreamsFilterTest() {
        Student streamsFilterStudent = StreamsFilterStudent("小三");
        System.out.println(streamsFilterStudent);
    }

    public Student StreamsFilterStudent(String name){
        return getStudents().stream()
                .filter(student -> name.equals(student.getName()))
                .findAny()
                .orElse(null);//如果没有返回null
    }

多条件判断

   public List<Student> getStudents(){
        List<Student> result = new ArrayList<>();
        result.add(new Student(1,"小易","21"));
        result.add(new Student(2,"小二","23"));
        result.add(new Student(4,"小二","24"));
        result.add(new Student(3,"小二","24"));
        return result;
    }

    @Test
    public void StreamsFilterTest() {
        Student streamsFilterStudent = StreamsFilterStudent();
        System.out.println(streamsFilterStudent);
        System.out.println("==============================");
        System.out.println(StreamsFilterStudents());
    }

    public Student StreamsFilterStudent(){
        return getStudents().stream()
                .filter(student -> {
                    if (student != null && "24".equals(student.getAge())) {
                        return true;
                    }return false;
                })
                .findAny()
                .orElse(null);//如果没有返回null
    }

    public Student StreamsFilterStudents(){
        return getStudents().stream()
                .filter(student -> {
                    if (student != null && "21".equals(student.getAge())) {
                        return true;
                    }
                    return false;
                })
                .findAny()
                .orElse(null);//如果没有返回null
    }

stream和map

如果使用map它返回的类型是Stirng

    public List<Student> getStudents(){
        List<Student> result = new ArrayList<>();
        result.add(new Student(1,"小易","21"));
        result.add(new Student(2,"小二","23"));
        result.add(new Student(4,"小二","24"));
        result.add(new Student(3,"小二","24"));
        return result;
    }

    @Test
    public void StreamsFilterTest() {
        String studentName = StreamsFilterStudent("小二");
        System.out.println("名字====>"+studentName);

    }

    public String StreamsFilterStudent(String name){

        String studentName = getStudents().stream()
                .filter(student -> name.equals(student.getName()))
                .map(Student::getName)
                .findAny()
                .orElse(null);//如果没有返回null
        System.out.println(studentName);
        return studentName;
    }

stream和list

    public List<Student> getStudents(){
        List<Student> result = new ArrayList<>();
        result.add(new Student(1,"小易","21"));
        result.add(new Student(2,"小二","23"));
        result.add(new Student(4,"小二","24"));
        result.add(new Student(3,"小二","24"));
        return result;
    }

    @Test
    public void StreamsFilterTest() {
        List studentName = StreamsFilterStudent("小二");
        System.out.println("名字====>"+studentName);

    }

    public List StreamsFilterStudent(String name){
        List<String> studentList = getStudents().stream()
                .map(Student::getName)
                .limit(1)//可以指定返回多少条
                .collect(Collectors.toList());

//        studentList.forEach(s -> System.out.println(s));
        studentList.forEach(System.out::println);
        return studentList;
    }

具体可参照oracle
https://www.oracle.com/technical-resources/articles/java/ma14-java-se-8-streams.html

并行流

打印数字 可以看到到时按照顺序排序打印下来的

    public static void main(String[] args) {
        System.out.println("normal");
        IntStream range = IntStream.rangeClosed(1, 100);
        range.forEach(System.out::println);

        System.out.println("parallel");
        IntStream intStream = IntStream.rangeClosed(1, 99);
        intStream.forEach(System.out::println);
    }

打印26个字母

    @Test
    public void getDataTest(){
        List<String> data = getData();
        data.stream().forEach(System.out::println);
    }

    public List<String> getData(){
        List<String> data = new ArrayList<>();
        int n = 97; //97=a 122=z
        while (n<=122){
            char c = (char) n;
            data.add(String.valueOf(c));
            n++;
        }
        return data;
    }

Stream 是否以并行模式运行

可以是用paraller

    public static void main(String[] args) {
        System.out.println("normal");
        IntStream range = IntStream.rangeClosed(1, 100);
        range.forEach(System.out::println);

        System.out.println("parallel");
        IntStream intStream = IntStream.rangeClosed(1, 99);
        IntStream parallel = intStream.parallel();
        System.out.println(parallel.isParallel());//true为开启
        parallel.forEach(System.out::println);
    }

可加入线程名


    public static void main(String[] args) {
        System.out.println("normal");
        IntStream range = IntStream.rangeClosed(1, 100);
        range.forEach(p->{
            System.out.println("Thread : "+ Thread.currentThread().getName() + " "+ p);
        });

        System.out.println("parallel");
        IntStream intStream = IntStream.rangeClosed(1, 99);
        IntStream parallel = intStream.parallel();
        System.out.println(parallel.isParallel());//true为开启
        parallel.forEach(p->{
            System.out.println("Thread : "+Thread.currentThread().getName() + " "+ p);
        });
    }

    @Test
    public void getDataTest(){
        List<String> data = getData();
        data.stream().forEach(System.out::println);
    }

    public List<String> getData(){
        List<String> data = new ArrayList<>();
        int n = 97; //97=a 122=z
        while (n<=122){
            char c = (char) n;
            data.add(String.valueOf(c));
            n++;
        }
        return data;
    }

待更新

posted @ 2021-12-17 17:50  Rzk  阅读(90)  评论(0编辑  收藏  举报