Java8使用Stream实现List操作

JAVA8使用Stream实现List操作

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

完整示例:

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<Student> 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<Student> 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<Student> 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<String> resList3 = studentList.stream().map(student -> student.getName()).collect(Collectors.toList());
        System.out.println("resList3 : "+ resList3);
        // resList3 : [xiaoming, xiaoli, xiaowang, xiaogang, xiaozhao]
 
        //取出所有学生的年龄相加
        double sumAge = studentList.stream().mapToDouble(item ->item.getAge()).sum();
        
		//取出所有学生的年龄最大的学生的年龄
		double  macAge =  studentList.stream().max(Comparator.comparingDouble(Student::getAge)).get().getAge();
		
        //取出所有学生的名字
        long start = System.currentTimeMillis();
        List<Student> 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();
        // stream替换成parallelStream实现并发 但只有在大数据量的list下可以提高效率 一般情况使用stream即可
        List<Student> 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
        
	// 数据排序
     List<Student> resList8 = studentList.stream().sorted((s1,s2) -> s1.getAge().compareTo(s2.getAge())).collect(Collectors.toList());
    }
    //按年龄倒序
    ArrayList<Student> demoArray = (ArrayList<Student>)studentList;
    demoArray .sort(Comparator.comparing(Student::getAge).reversed());
    //倒序2
    Comparator<Student> comparator = (h1,h2) -> h1.getAge().compareTo(h2.getAge());
    demoArray.sort(comparator.reversed());
    //多条件倒叙
    studentList.sort(Comparator.comparing(Student::getSex).reversed().thenComparing(Student::getAge));
    //多条件正序
    studentList.sort(Comparator.comparing(Student::getSex).thenComparing(Student::getAge));
    //分组
    Map<String, List<Demo>> collect = studentList.stream().collect(Collectors.groupingBy(Student::getAge));
}

完整控制台显示:

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

相关链接
Java8 中通过 Stream 对列表分组的groupBy 的使用
Java8 中通过 Stream 对列表进行去重的方法

你可能感兴趣的:(spring,list,数据结构,java)