java流stream

Java流Stream小结

概括

常用函数:

用例解释:

举例说明:

数据结果转换:

代码:

概括:

使用集合的时候会对集合进行迭代,比如for循环或者iterator;

使用stream函数可以简化迭代过程
stream没有改变集合内容,只是描述集合,没有改变集合。

对象------>流------>对象

所以在操作的时候要注意转换的是流,不能是对象

常用函数:

Map,List,Set,String,IntSummaryStatistics;
stream,filter,flatmap,sorted,map,max,min,summaryStatistics;
collect,count,Collectors,get

输入对象通常是集合对象Map,List,Set;输出除了集合以外还有常用的String,或者对集合统计IntSummaryStatistics;

stream将集合转换成流,filter用于过滤,sorted用于排序,flatmap用于流合并,map用于流内容转换,还有max,min,summaryStatistics等辅助函数

collect,count,get都是用于将结果从流转换成对象

举例说明:

List<Student> students = new ArrayList<Student>();
students.add(Student.builder().name("小明").age(15).sex("M").build());
students.add(Student.builder().name("小红").age(20).sex("F").build());
students.add(Student.builder().name("小白").age(20).sex("M").build());


List<Student> studentsres = students.stream()
    	.filter(Objects::nonNull)
        .filter(o->o.getSex().equals("F"))
        .filter(o->o.getSex().equals("M"))
        .filter(o->o.getAge()>18)
        .collect(Collectors.toList());

Set<String> namesset = students.stream()
         .map(o->o.getName())
         .collect(Collectors.toSet());
System.out.println("namesset:"+names);//names:[小明, 小红, 小白]

先将对象转换成流,过滤函数过滤,注意留下都是需要的;

如果过滤内容为null,则需要用这样的写法.filter(Objects::nonNull)

filter里面的o都是已经经过类型转换成Student,而Object则没有经过类型转换

map函数会将流转换成另一种流,最后对应输出对象也要转换成不同的流

数据结果转换:

Collectors.toList() 转换成list

Collectors.toMap() 转换成map

Collectors.toSet() 转换成set

Collectors.joining() 转换成string

代码:

public class SteamDemo {
    public static void main(String[] args) {
        //使用集合的时候会对集合进行迭代,比如for循环或者iterator;
        //使用stream函数可以简化迭代过程
        //stream没有改变集合内容,知识描述集合
        List<Student> students = new ArrayList<Student>();
        students.add(Student.builder().name("小明").age(15).sex("M").build());
        students.add(Student.builder().name("小红").age(20).sex("F").build());
        students.add(Student.builder().name("小白").age(20).sex("M").build());

        List<Student> students2 = new ArrayList<Student>();
        students2.add(Student.builder().name("小王").age(14).sex("F").build());
        students2.add(Student.builder().name("小张").age(25).sex("M").build());

        //1.过滤,filter
        List<Student> studentsres = students.stream()
                .filter(Objects::nonNull)
                .filter(o->o.getSex().equals("F"))
                .filter(o->o.getSex().equals("M"))
                                            .filter(o->o.getAge()>18)
                                            .collect(Collectors.toList());
        System.out.println(studentsres);//[Student(name=小红, sex=F, age=20), Student(name=小白, sex=M, age=20)]

        //2.计数,count
        long count = students.stream()
                             .filter(o -> o.getSex().equals("M"))
                             .count();
        System.out.println("count:"+count);//count:2

        //3.数据格式转换,获取里面的姓名,map
        List<String> names = students.stream()
                                     .map(o->o.getName())
                                     .collect(Collectors.toList());
        System.out.println("names:"+names);//names:[小明, 小红, 小白]

        Set<String> namesset = students.stream()
                .map(o->o.getName())
                .collect(Collectors.toSet());
        System.out.println("namesset:"+names);//names:[小明, 小红, 小白]



        //4.合并,flatMap
        List<Student> studentall = Stream.of(students,students2)
                                         .flatMap(o->o.stream())
                                         .collect(Collectors.toList());
        System.out.println(studentall);//[Student(name=小明, sex=M, age=15), Student(name=小红, sex=F, age=20), Student(name=小白, sex=M, age=20), Student(name=小王, sex=F, age=22), Student(name=小张, sex=M, age=25)]

        //5.min和max
        Student minstudent = studentall.stream()
                                        .filter(o->o.getSex().equals("M"))
                                        .min(Comparator.comparing(o->o.getAge()))
                                        .get();
        System.out.println("minstudent:"+minstudent);//minstudent:Student(name=小红, sex=F, age=20)


         //5.累加器求值,reduce
        int sum = studentall.stream()
                                .mapToInt(o->o.getAge())
                                .reduce(0,(acc,element)->acc+element);

        System.out.println("sum:"+sum);//sum:102


        //6.极值的各种统计,返回结果为值summaryStatistics()
        IntSummaryStatistics studentssummary = students.stream()
                                                        .mapToInt(student->student.getAge())
                                                        .summaryStatistics();
        System.out.printf("studentssummary:max->%d,min->%d,sum->%d,average->%f",
                studentssummary.getMax(),
                studentssummary.getMin(),
                studentssummary.getSum(),
                studentssummary.getAverage());
        System.out.println();

        //7.排序,sort
        System.out.println("******7.排序***********");
        System.out.println(studentall);
        List<Student> studentorder = studentall.stream()
                                                .sorted((o1,o2)->o1.getAge()-o2.getAge())
                                                .collect(Collectors.toList());
        System.out.println("studentorder:"+studentorder);


        //8.修改流里面的内容,join
        System.out.println("***********8.修改流里面的内容,join***********");
        String namesChange = studentall.stream()
                                                .map(Student::getName)
                                                .collect(Collectors.joining("-1",",",","));
        System.out.println(namesChange);


        //9.并行,parallelStream
        System.out.println("***********9.过滤,parallelStream***********");
        List<Student> studentsres2 = studentall.parallelStream()
                .filter(o->o.getSex().equals("F"))
                .filter(o->o.getSex().equals("M"))
                .filter(o->o.getAge()>18)
                .collect(Collectors.toList());
        System.out.println(studentsres2);//[Student(name=小红, sex=F, age=20), Student(name=小白, sex=M, age=20)]






    }
}

posted @ 2022-04-04 19:35  yorkiiz  阅读(40)  评论(0编辑  收藏  举报