Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check statement for every list item

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?

like image 668
barmi Avatar asked Feb 03 '17 09:02

barmi


1 Answers

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()
like image 135
Jon Skeet Avatar answered Oct 15 '22 03:10

Jon Skeet