I have a const experience
value, person
object, list of skill
and method (can not modify it) hasSkill(skill,person,experience)
which returns boolean.
I want to check that person
has every skill from the list.
My code is:
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);
}
I am pretty sure that there is better solution but can not find it; what should I do to fix my code?
It sounds like you want allMatch
:
return skillList.stream().allMatch(s -> hasSingleSkill(s, person));
As another more general matter, any time you have
condition ? true : false
you can just replace that with
condition
So your existing code of
(long-expression).size() == skillList.size() ? true : false
could be simplified to
(long-expression).size() == skillList.size()
See more on this question at Stackoverflow