Java列表流anyMatch返回不正确的结果

时间:2014-08-09 11:24:34

标签: java jpa arraylist java-8 java-stream

我有一个JPA课程' Project'其中包含'Employee'列表,getEmployeeList()会返回Employees列表。

public  class Project implements Serializable {

    @ManyToMany(targetEntity=Employee.class)
    private List<Employee> employeeList;

    public List<Employee> getEmployeeList() {
        return this.employeeList;
    }
}

public  class Employee implements Serializable {
    @Basic
    private EmployeeType type;

    public EmployeeType getType() {
        return this.type;
    }
}

public enum EmployeeType implements Serializable{
  EDITOR,
  AUDITOR,
  ADMIN,
  INACTIVE
}

我的员工类型为“审核员”。当我运行以下

for(Employee e: p.getEmployeeList()){
     if(e.getType()==EmployeeType.AUDITOR)
         found=true;
 }

它返回true应该是什么,但是当我运行

(p.getEmployeeList()).stream().anyMatch((e) -> (e.getType()==EmployeeType.AUDITOR))

返回false

我做错了什么?

2 个答案:

答案 0 :(得分:0)

解决方案 - 将返回类型更改为arraylist

public ArrayList<Employee> getEmployeeList() {
    return new ArrayList<>(this.employeeList);
}

答案 1 :(得分:0)

更新您的EclipseLink版本,此错误已修复!

错误:https://bugs.eclipse.org/bugs/show_bug.cgi?id=433075

更新至2.6.0或更高版本:http://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink/2.6.0