Java 如何检查hashmap中的所有员工是否都有姓名?
我有一个员工的哈希图:Java 如何检查hashmap中的所有员工是否都有姓名?,java,collections,hashmap,logic,Java,Collections,Hashmap,Logic,我有一个员工的哈希图: Employee{ String name; String id; String Salary; } Map<String,Employee> emps = new HashMap<>(); emps.put("1",employee1); emps.put("2",employee2); emps.put("3",employee3); 员工{ 字符串名; 字符串id; 串薪; } Map emps=newhashmap(); E
Employee{
String name;
String id;
String Salary;
}
Map<String,Employee> emps = new HashMap<>();
emps.put("1",employee1);
emps.put("2",employee2);
emps.put("3",employee3);
员工{
字符串名;
字符串id;
串薪;
}
Map emps=newhashmap();
EMP.put(“1”,雇员1);
emps.put(“2”,雇员2);
emps.put(“3”,雇员3);
我希望有以下场景:
如何编写这样的场景?您可以使用
流
s筛选有或没有姓名的员工,对他们进行计数,并将结果与列表大小进行比较
long count = emps.values()
.stream()
.filter(employee -> employee.getName() != null)
.count();
/**
* count == 0 => All employess dont have name
* count == size => All employees have name
*/
return count == 0 || count == employees.size();
您可以使用
Stream
s筛选有或没有姓名的员工,对他们进行计数,并将结果与列表大小进行比较
long count = emps.values()
.stream()
.filter(employee -> employee.getName() != null)
.count();
/**
* count == 0 => All employess dont have name
* count == size => All employees have name
*/
return count == 0 || count == employees.size();
公共类通行证{
公共静态void main(字符串[]args){
列表=新的ArrayList();
布尔res=真;
对于(员工e:列表){
res=res&&isPass(e);
}
系统输出打印项次(res);
}
公共静态布尔值isPass(员工){
if(employee==null)
返回false;
if(employee.getName()==null)
返回true;
if(employee.getName()!=null&&!StringUtils.isEmpty(employee.getName()){
返回true;
}
返回false;
}
}公共类通行证{
公共静态void main(字符串[]args){
列表=新的ArrayList();
布尔res=真;
对于(员工e:列表){
res=res&&isPass(e);
}
系统输出打印项次(res);
}
公共静态布尔值isPass(员工){
if(employee==null)
返回false;
if(employee.getName()==null)
返回true;
if(employee.getName()!=null&&!StringUtils.isEmpty(employee.getName()){
返回true;
}
返回false;
}
}您可以通过调用(map.values())对映射的值进行迭代,这将为您提供集合。然后应用您的逻辑
Collection<Employee> values = emps.values();
int count = 0;
for (Employee employee : values) {
if(null != employee.name){
count ++;
}
}
return count == 0 || count == emps.size();
Collection values=emps.values();
整数计数=0;
for(员工:价值观){
if(null!=employee.name){
计数++;
}
}
返回计数==0 | |计数==emps.size();
您可以通过调用(map.values())对映射的值进行迭代,这将为您提供集合。然后应用您的逻辑
Collection<Employee> values = emps.values();
int count = 0;
for (Employee employee : values) {
if(null != employee.name){
count ++;
}
}
return count == 0 || count == emps.size();
Collection values=emps.values();
整数计数=0;
for(员工:价值观){
if(null!=employee.name){
计数++;
}
}
返回计数==0 | |计数==emps.size();
1)获取员工列表
Collection<Employee> employees = employeesMap.values();
testEmployees(employees);
1) 获取员工列表
Collection<Employee> employees = employeesMap.values();
testEmployees(employees);
你能告诉我们你试过什么吗?你地图的钥匙是什么<代码>emps.put(Employee)将不编译我回答了您更新的问题,但如果员工未通过要求,将抛出异常。您能告诉我们您尝试了什么吗?您地图的关键是什么<代码>EMP.put(员工)将不编译我回答您,如果员工未通过要求,将抛出更新的问题,异常情况。@TmP同样的逻辑适用,但您需要手动迭代集合。@TmP同样的逻辑适用,但您需要手动迭代集合。您无法区分“全部为空”或“无为空”null'@StefanWarminski&&ed假设第一个员工的名字是
null
->res=false
。现在,即使第二个emp有一个名称,您也无法区分“全部为空”或“无一为空”@StefanWarminski&&edlet假设第一个员工的名称为null
->res=false
。现在,即使第二个emp有一个名称,其他emp也不会进入isPass
和res
stayfalse