【学习笔记】JAVA8使用Stream实现List操作

JAVA8使用Stream实现List操作

  • Java 8引入了全新的Stream API。这里的Stream和I/O流不同,它更像具有Iterable的集合类,但行为和集合类又有所不同。
  • stream是对集合对象功能的增强,它专注于对集合对象进行各种非常便利、高效的聚合操作,或者大批量数据操作。
  • 只要给出需要对其包含的元素执行什么操作,比如 “过滤掉长度大于 10 的字符串”、“获取每个字符串的首字母”等,Stream 会隐式地在内部进行遍历,做出相应的数据转换。

Filter 过滤器

遍历数据并检查其中的元素时使用

  //过滤所有18岁的学生
  List resList = 
studentList.stream().filter(student -> 18 ==student.getAge()).collect(Collectors.toList());
  System.out.println("resList : "+ resList);
 // resList : [Student(name=xiaoming, age=18, sex=男, teacher=mr.li, clazz=no.1),     
  // Student(name=xiaoli, age=18, sex=女, teacher=mr.li, clazz=no.1)]
 
  //过滤所有18岁的男性学生
  List resList2 = 
studentList.stream().filter(student -> ("男".equals(student.getSex())) && student.getAge() == 18).collect(Collectors.toList());
  System.out.println("resList2 : "+ resList2);
 // resList2 : [Student(name=xiaoming, age=18, sex=男, teacher=mr.li, clazz=no.1)]

Map

map生成的是个一对一映射,for的作用

  //取出所有学生的名字
  List resList3 = 
   studentList.stream().map(student -> student.getName()).collect(Collectors.toList());
  System.out.println("resList3 : "+ resList3);
  // resList3 : [xiaoming, xiaoli, xiaowang, xiaogang, xiaozhao]

Collect

collect在流中生成列表,map,等常用的数据结构

  //取出所有学生的年龄
  List resList4 = 
    studentList.stream().map(student -> student.getAge()).collect(Collectors.toList());
  Set resSet5 = 
    studentList.stream().map(student -> student.getAge()).collect(Collectors.toSet());
  System.out.println("resList4 : "+ resList4);
  System.out.println("resSet5 : "+ resSet5);
  // resList4 : [18, 18, 20, 17, 21]
  // resSet5 : [17, 18, 20, 21]

并发

stream替换成parallelStream实现并发 但只有在大数据量的list下可以提高效率 一般情况使用stream即可

  //取出所有学生的名字
  long start = System.currentTimeMillis();
  List resList6 = 
    studentList.stream().filter(student -> ("男".equals(student.getSex())) && student.getAge() == 18).collect(Collectors.toList());
  System.out.println("resList6 time: "+ (System.currentTimeMillis()-start));

  long start1 = System.currentTimeMillis();
  List resList7 = studentList.parallelStream().filter(student ->         ("男".equals(student.getSex())) && student.getAge() == 18).collect(Collectors.toList());
  System.out.println("resList7 time: "+ (System.currentTimeMillis()-start1));

    //resList6 time: 0
    //resList7 time: 10

完整示例:

public class TestStream {

    private static Student s1;
    private static Student s2;
    private static Student s3;
    private static Student s4;
    private static Student s5;
    private static List studentList;

    static {
        s1 = new Student("xiaoming",18,"男","mr.li","no.1");
        s2 = new Student("xiaoli",18,"女","mr.li","no.1");
        s3 = new Student("xiaowang",20,"女","mr.hu","no.2");
        s4 = new Student("xiaogang",17,"女","mr.hu","no.2");
        s5 = new Student("xiaozhao",21,"男","mr.wu","no.3");
        studentList = Arrays.asList(s1,s2);
    }


    public static void main(String[] args){
        //过滤所有18岁的学生
        List resList = studentList.stream().filter(student -> 18 == student.getAge()).collect(Collectors.toList());
        System.out.println("resList : "+ resList);
        // resList : [Student(name=xiaoming, age=18, sex=男, teacher=mr.li, clazz=no.1), Student(name=xiaoli, age=18, sex=女, teacher=mr.li, clazz=no.1)]

        //过滤所有18岁的男性学生
        List resList2 = studentList.stream().filter(student -> ("男".equals(student.getSex())) && student.getAge() == 18).collect(Collectors.toList());
        System.out.println("resList2 : "+ resList2);
        // resList2 : [Student(name=xiaoming, age=18, sex=男, teacher=mr.li, clazz=no.1)]

        //取出所有学生的名字
        List resList3 = studentList.stream().map(student -> student.getName()).collect(Collectors.toList());
        System.out.println("resList3 : "+ resList3);
        // resList3 : [xiaoming, xiaoli, xiaowang, xiaogang, xiaozhao]

        //取出所有学生的年龄
        List resList4 = studentList.stream().map(student -> student.getAge()).collect(Collectors.toList());
        Set resSet5 = studentList.stream().map(student -> student.getAge()).collect(Collectors.toSet());
        System.out.println("resList4 : "+ resList4);
        System.out.println("resSet5 : "+ resSet5);
        // resList4 : [18, 18, 20, 17, 21]
        // resSet5 : [17, 18, 20, 21]

        //取出所有学生的名字
        long start = System.currentTimeMillis();
        List resList6 = studentList.stream().filter(student -> ("男".equals(student.getSex())) && student.getAge() == 18).collect(Collectors.toList());
        System.out.println("resList6 time: "+ (System.currentTimeMillis()-start));

        long start1 = System.currentTimeMillis();
        List resList7 = studentList.parallelStream().filter(student -> ("男".equals(student.getSex())) && student.getAge() == 18).collect(Collectors.toList());
        System.out.println("resList7 time: "+ (System.currentTimeMillis()-start1));

        //resList6 time: 0
        //resList7 time: 10

    }

}

完整控制台显示

resList : [Student(name=xiaoming, age=18, sex=男, teacher=mr.li, clazz=no.1), Student(name=xiaoli, age=18, sex=女, teacher=mr.li, clazz=no.1)]
resList2 : [Student(name=xiaoming, age=18, sex=男, teacher=mr.li, clazz=no.1)]
resList3 : [xiaoming, xiaoli]
resList4 : [18, 18]
resSet5 : [18]
resList6 time: 1
resList7 time: 13

Collector静态方法:

【学习笔记】JAVA8使用Stream实现List操作_第1张图片

参考:https://www.jianshu.com/p/9fe8632d0bc2

           https://www.cnblogs.com/jobbible/p/9999574.html

你可能感兴趣的:(学习笔记)