gpt4 book ai didi

java - 如何从员工 map Prop 中检索ID值

转载 作者:行者123 更新时间:2023-12-01 14:17:18 25 4
gpt4 key购买 nike

下面是接受Employee对象列表的方法,我需要传递在雇员对象内部的map obejct的id值。我是Java8的新手,请看以下内容:

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;
}

员工JSON
{
"name": "name1",
"props": {
"id": "123", //this value has to retreive
"field2": "hey"
}
}

我需要在isIdValid方法中传递empName和“id”值,请问如何?

最佳答案

您可以将entrySet用作:

return employees.stream()
.flatMap(e -> e.getProps().entrySet().stream())
.filter(s -> s.getKey().equals("id"))
.allMatch(e -> isIdValid(idValue).test(e.getValue()));

编辑:-随着更新的问题,似乎您正在寻找
return employees.stream()
.filter(emp -> emp.getProps().containsKey("id"))
.allMatch(emp -> isIdValid(emp.getName(),
(String) emp.getProps().get("id")));

关于java - 如何从员工 map Prop 中检索ID值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60507196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com