我有一个const experience
值,person
对象,skill
列表和方法(不能修改它)hasSkill(skill,person,experience)
,返回布尔值。
我想检查person
是否具有列表中的所有技能。
我的代码是:
int experience = 5;
private hasAllSkills(person){
return skillList.stream().filter(s -> hasSingleSkill(s,person)).collect(Collectors.toList()).size() == skillList.size() ? true : false;
}
private boolean hasSingleSkill(Skill s, Person p){
return hasSkill(s,p,experience);
}
我很确定有更好的解决方案但找不到它;我该怎么做才能修复我的代码?
答案 0 :(得分:8)
听起来你想要allMatch
:
return skillList.stream().allMatch(s -> hasSingleSkill(s, person));
另一个更一般的问题,任何时候你
condition ? true : false
你可以用
替换它condition
所以你现有的代码
(long-expression).size() == skillList.size() ? true : false
可以简化为
(long-expression).size() == skillList.size()