1 2 3 4 5 6 7 8 9 |
List<Student> students = new ArrayList<>(); students.add(new Student(1,"张三",90)); students.add(new Student(2,"李四",60)); students.add(new Student(3,"王五",30)); students.add(new Student(4,"赵六",85)); int studentId = 3; Student student = students.stream().filter(o -> o.getId() == studentId).findAny().orElse(null); |
如上,获取id为3的元素对象,如果不存在返回null。
student类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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... } |
from:https://www.csdn.net/tags/MtTaAgwsOTU2MzEyLWJsb2cO0O0O.html