java8 stream:从集合中获取符合条件的元素


原文地址:https://blog.csdn.net/q649381130/article/details/84338490

目录

    List<Student> studentList = new ArrayList<>();
    studentList.add(new Student(1, "张三", 90));
    studentList.add(new Studnet(2, "李四", 60));
    studentList.add(new Student(3, "王五", 30));
    studentList.add(new Student(4, "赵六", 85));
    int studentId = 3;
    Student student = studentList.stream().filter(o -> o.getId() == studentId).findAny().orElse(null);
    

    如上,获取id为3的元素对象,如果不存在返回null

    Student类:

    public class Student {
        private int id;
        private String name;
        private int score;
        public Student(int id, String name, int score) {
           this.id = id;
           this.name = name;
           this.score = score;
        }
        get...
        set...
    }
    


    免责声明!

    本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



     
    粤ICP备18138465号  © 2018-2025 CODEPRJ.COM