john :
Below is the method which accepts a List of Employee objects, I need to pass id value of map obejct, which is inside employee object. I am new to Java8, pls look into below:
class Service {
void meth(List<Employee> employees){
employees.stream().flatMap(e -> map = e.getProps())
.keySet().stream())
.filter(s -> s.equals("id"))
.allMatch(idValue -> isIdValid(empName, idValue).test(id)); //how to pass empName and "id" Value here from employee map props object??
}
boolean isIdValid(String name, String id){
//validation of id here
}
}
class Employee{
String name;
Map<String, Object> props;
}
Employee JSON
{
"name": "name1",
"props": {
"id": "123", //this value has to retreive
"field2": "hey"
}
}
I need to pass empName and "id" values in isIdValid method, how pls?
Naman :
You can use the entrySet
as:
return employees.stream()
.flatMap(e -> e.getProps().entrySet().stream())
.filter(s -> s.getKey().equals("id"))
.allMatch(e -> isIdValid(idValue).test(e.getValue()));
Edit:- With the updated question , seems like you're looking for
return employees.stream()
.filter(emp -> emp.getProps().containsKey("id"))
.allMatch(emp -> isIdValid(emp.getName(),
(String) emp.getProps().get("id")));