Stream()——报错使用final变量解决办法
问题:
在使用Stream()流进行操作变量的时候,会不时碰到:variable used in lambda expression should be final or effectively final,这是因为在Java 8 之前,匿名类中如果要访问局部变量的话,那个局部变量必须显式的声明为final。
解决办法:
(小白一个,勿喷)声明一个final变量进行重新赋值再进行流操作。
String changeIdList = jsonObject.getString("changeIdList");
// 局部变量
List
if (!changeIdList.equals("[]")){
List
Student student = new Student();
student.setDeptIds(longs);
changeStudents = StudentService.selectStudentList(student);
}
// 重新赋值
final List
List
!(changeStudentList.stream().map(Student::getSid).collect(Collectors.toList()).contains(o.getSid()))
).collect(Collectors.toList());