pallavt pallavt - 10 days ago 6
Java Question

Java List stream anyMatch returning incorrect result

I have a JPA class 'Project' which has a list of

'Employee'
and the
getEmployeeList()
returns the list of
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
}


I have an Employee with type 'Auditor' and when I run the following

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


it returns true which it should, but when I run

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


it returns false

What am I doing wrong?

Answer

Solution - change the return type to arraylist

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