From 54356efd356fc920df2c62d737a34548634a1cac Mon Sep 17 00:00:00 2001 From: FroGitHub Date: Fri, 17 Jan 2025 18:52:10 +0200 Subject: [PATCH 1/7] implement all methods --- .../java/practice/CandidateValidator.java | 38 +++++++++++++- src/main/java/practice/StreamPractice.java | 50 ++++++++++++++++--- 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 8d2e56c0e..31e161d01 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -1,5 +1,39 @@ package practice; -public class CandidateValidator { - //write your code here +import java.util.Arrays; +import java.util.List; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import model.Candidate; + +public class CandidateValidator implements Predicate { + @Override + public boolean test(Candidate candidate) { + if (candidate == null) { + return false; + } + if (!"Ukrainian".equals(candidate.getNationality())) { + return false; + } + if (candidate.getAge() < 35) { + return false; + } + if (!candidate.isAllowedToVote()) { + return false; + } + try { + List periodInUkraine = Arrays.stream(candidate.getPeriodsInUkr().split("-")) + .map(String::trim) + .map(Integer::valueOf) + .collect(Collectors.toList()); + if (periodInUkraine.size() != 2 + || periodInUkraine.get(1) - periodInUkraine.get(0) < 10) { + return false; + } + } catch (NumberFormatException | IndexOutOfBoundsException e) { + return false; + } + return true; + } } + diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 57b1ca2e2..528e6e326 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,8 +1,12 @@ package practice; -import java.util.Collections; +import java.util.Arrays; import java.util.List; +import java.util.NoSuchElementException; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import model.Candidate; +import model.Cat; import model.Person; public class StreamPractice { @@ -14,7 +18,16 @@ public class StreamPractice { * "Can't get min value from list: < Here is our input 'numbers' >" */ public int findMinEvenNumber(List numbers) { - return 0; + return numbers.stream() + .map(s -> s.split(",")) + .flatMap(Arrays::stream) + .map(String::trim) + .map(Integer::valueOf) + .filter(i -> i % 2 == 0) + .min(Integer::compareTo) + .orElseThrow(() -> new RuntimeException( + "Can't get min value from list: " + numbers + )); } /** @@ -23,7 +36,12 @@ public int findMinEvenNumber(List numbers) { * But before that subtract 1 from each element on an odd position (having the odd index). */ public Double getOddNumsAverage(List numbers) { - return 0D; + + return IntStream.range(0, numbers.size()) + .map(i -> i % 2 == 1 ? numbers.get(i) - 1 : numbers.get(i)) + .filter(num -> num % 2 != 0) + .average() + .orElseThrow(NoSuchElementException::new); } /** @@ -35,7 +53,10 @@ public Double getOddNumsAverage(List numbers) { * Example: select men who can be recruited to army (from 18 to 27 years old inclusively). */ public List selectMenByAge(List peopleList, int fromAge, int toAge) { - return Collections.emptyList(); + return peopleList.stream() + .filter(p -> p.getAge() >= fromAge && p.getAge() <= toAge) + .filter(p -> p.getSex() == Person.Sex.MAN) + .collect(Collectors.toList()); } /** @@ -50,7 +71,11 @@ public List selectMenByAge(List peopleList, int fromAge, int toA */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { - return Collections.emptyList(); + return peopleList.stream() + .filter(p -> p.getAge() >= fromAge) + .filter((p -> (p.getSex() == Person.Sex.WOMAN && p.getAge() <= femaleToAge) + || (p.getSex() == Person.Sex.MAN && p.getAge() <= maleToAge))) + .collect(Collectors.toList()); } /** @@ -59,10 +84,15 @@ public List getWorkablePeople(int fromAge, int femaleToAge, * return the names of all cats whose owners are women from `femaleAge` years old inclusively. */ public List getCatsNames(List peopleList, int femaleAge) { - return Collections.emptyList(); + return peopleList.stream() + .filter(p -> p.getSex() == Person.Sex.WOMAN) + .filter(p -> p.getAge() >= femaleAge) + .flatMap(c -> c.getCats().stream()) + .map(Cat::getName) + .collect(Collectors.toList()); } - /** + /** V - wtf MA, wrong article * Your help with a election is needed. Given list of candidates, where each element * has Candidate.class type. * Check which candidates are eligible to apply for president position and return their @@ -75,6 +105,10 @@ public List getCatsNames(List peopleList, int femaleAge) { * parametrized with Candidate in CandidateValidator. */ public List validateCandidates(List candidates) { - return Collections.emptyList(); + return candidates.stream() + .filter(new CandidateValidator()) + .map(Candidate::getName) + .sorted() + .collect(Collectors.toList()); } } From 2d0550a1277c5f3e9276e42ed27eedf5c197e0f6 Mon Sep 17 00:00:00 2001 From: FroGitHub Date: Fri, 17 Jan 2025 19:03:37 +0200 Subject: [PATCH 2/7] correcting my code --- .../java/practice/CandidateValidator.java | 9 ++++++--- src/main/java/practice/StreamPractice.java | 20 ++++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 31e161d01..683be9f58 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -7,15 +7,18 @@ import model.Candidate; public class CandidateValidator implements Predicate { + private static final int neededAge = 35; + private static final int neededPeriod = 10; + @Override public boolean test(Candidate candidate) { if (candidate == null) { return false; } - if (!"Ukrainian".equals(candidate.getNationality())) { + if (!candidate.getNationality().equals("Ukrainian")) { return false; } - if (candidate.getAge() < 35) { + if (candidate.getAge() < neededAge) { return false; } if (!candidate.isAllowedToVote()) { @@ -27,7 +30,7 @@ public boolean test(Candidate candidate) { .map(Integer::valueOf) .collect(Collectors.toList()); if (periodInUkraine.size() != 2 - || periodInUkraine.get(1) - periodInUkraine.get(0) < 10) { + || periodInUkraine.get(1) - periodInUkraine.get(0) < neededPeriod) { return false; } } catch (NumberFormatException | IndexOutOfBoundsException e) { diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 528e6e326..555520d08 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,6 +1,7 @@ package practice; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.NoSuchElementException; import java.util.stream.Collectors; @@ -18,6 +19,9 @@ public class StreamPractice { * "Can't get min value from list: < Here is our input 'numbers' >" */ public int findMinEvenNumber(List numbers) { + if (numbers == null || numbers.isEmpty()) { + throw new RuntimeException("Can't get min value from list"); + } return numbers.stream() .map(s -> s.split(",")) .flatMap(Arrays::stream) @@ -36,7 +40,9 @@ public int findMinEvenNumber(List numbers) { * But before that subtract 1 from each element on an odd position (having the odd index). */ public Double getOddNumsAverage(List numbers) { - + if (numbers == null || numbers.isEmpty()) { + return 0D; + } return IntStream.range(0, numbers.size()) .map(i -> i % 2 == 1 ? numbers.get(i) - 1 : numbers.get(i)) .filter(num -> num % 2 != 0) @@ -53,6 +59,9 @@ public Double getOddNumsAverage(List numbers) { * Example: select men who can be recruited to army (from 18 to 27 years old inclusively). */ public List selectMenByAge(List peopleList, int fromAge, int toAge) { + if (peopleList == null || peopleList.isEmpty()) { + return Collections.emptyList(); + } return peopleList.stream() .filter(p -> p.getAge() >= fromAge && p.getAge() <= toAge) .filter(p -> p.getSex() == Person.Sex.MAN) @@ -71,6 +80,9 @@ public List selectMenByAge(List peopleList, int fromAge, int toA */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { + if (peopleList == null || peopleList.isEmpty()) { + return Collections.emptyList(); + } return peopleList.stream() .filter(p -> p.getAge() >= fromAge) .filter((p -> (p.getSex() == Person.Sex.WOMAN && p.getAge() <= femaleToAge) @@ -84,6 +96,9 @@ public List getWorkablePeople(int fromAge, int femaleToAge, * return the names of all cats whose owners are women from `femaleAge` years old inclusively. */ public List getCatsNames(List peopleList, int femaleAge) { + if (peopleList == null || peopleList.isEmpty()) { + return Collections.emptyList(); + } return peopleList.stream() .filter(p -> p.getSex() == Person.Sex.WOMAN) .filter(p -> p.getAge() >= femaleAge) @@ -105,6 +120,9 @@ public List getCatsNames(List peopleList, int femaleAge) { * parametrized with Candidate in CandidateValidator. */ public List validateCandidates(List candidates) { + if (candidates == null || candidates.isEmpty()) { + return Collections.emptyList(); + } return candidates.stream() .filter(new CandidateValidator()) .map(Candidate::getName) From 9a44c01b0a265beff79c8a34a26e1aa20746c480 Mon Sep 17 00:00:00 2001 From: FroGitHub Date: Sun, 19 Jan 2025 11:33:41 +0200 Subject: [PATCH 3/7] correcting my code x2 --- .../java/practice/CandidateValidator.java | 21 ++++++-------- src/main/java/practice/StreamPractice.java | 29 +++++-------------- 2 files changed, 16 insertions(+), 34 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 683be9f58..0313eb982 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -3,32 +3,29 @@ import java.util.Arrays; import java.util.List; import java.util.function.Predicate; -import java.util.stream.Collectors; import model.Candidate; public class CandidateValidator implements Predicate { + // National for being candidate + private static final String neededNational = "Ukrainian"; + // Minimum required age for being candidate private static final int neededAge = 35; + // Minimum required years for being candidate private static final int neededPeriod = 10; @Override public boolean test(Candidate candidate) { - if (candidate == null) { - return false; - } - if (!candidate.getNationality().equals("Ukrainian")) { - return false; - } - if (candidate.getAge() < neededAge) { - return false; - } - if (!candidate.isAllowedToVote()) { + if (candidate == null + || !candidate.getNationality().equals(neededNational) + || candidate.getAge() < neededAge + || !candidate.isAllowedToVote()) { return false; } try { List periodInUkraine = Arrays.stream(candidate.getPeriodsInUkr().split("-")) .map(String::trim) .map(Integer::valueOf) - .collect(Collectors.toList()); + .toList(); if (periodInUkraine.size() != 2 || periodInUkraine.get(1) - periodInUkraine.get(0) < neededPeriod) { return false; diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 555520d08..5551dc419 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,7 +1,6 @@ package practice; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.NoSuchElementException; import java.util.stream.Collectors; @@ -19,13 +18,9 @@ public class StreamPractice { * "Can't get min value from list: < Here is our input 'numbers' >" */ public int findMinEvenNumber(List numbers) { - if (numbers == null || numbers.isEmpty()) { - throw new RuntimeException("Can't get min value from list"); - } + return numbers.stream() - .map(s -> s.split(",")) - .flatMap(Arrays::stream) - .map(String::trim) + .flatMap(s -> Arrays.stream(s.split(","))) .map(Integer::valueOf) .filter(i -> i % 2 == 0) .min(Integer::compareTo) @@ -40,9 +35,7 @@ public int findMinEvenNumber(List numbers) { * But before that subtract 1 from each element on an odd position (having the odd index). */ public Double getOddNumsAverage(List numbers) { - if (numbers == null || numbers.isEmpty()) { - return 0D; - } + return IntStream.range(0, numbers.size()) .map(i -> i % 2 == 1 ? numbers.get(i) - 1 : numbers.get(i)) .filter(num -> num % 2 != 0) @@ -59,9 +52,7 @@ public Double getOddNumsAverage(List numbers) { * Example: select men who can be recruited to army (from 18 to 27 years old inclusively). */ public List selectMenByAge(List peopleList, int fromAge, int toAge) { - if (peopleList == null || peopleList.isEmpty()) { - return Collections.emptyList(); - } + return peopleList.stream() .filter(p -> p.getAge() >= fromAge && p.getAge() <= toAge) .filter(p -> p.getSex() == Person.Sex.MAN) @@ -80,9 +71,7 @@ public List selectMenByAge(List peopleList, int fromAge, int toA */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { - if (peopleList == null || peopleList.isEmpty()) { - return Collections.emptyList(); - } + return peopleList.stream() .filter(p -> p.getAge() >= fromAge) .filter((p -> (p.getSex() == Person.Sex.WOMAN && p.getAge() <= femaleToAge) @@ -96,9 +85,7 @@ public List getWorkablePeople(int fromAge, int femaleToAge, * return the names of all cats whose owners are women from `femaleAge` years old inclusively. */ public List getCatsNames(List peopleList, int femaleAge) { - if (peopleList == null || peopleList.isEmpty()) { - return Collections.emptyList(); - } + return peopleList.stream() .filter(p -> p.getSex() == Person.Sex.WOMAN) .filter(p -> p.getAge() >= femaleAge) @@ -120,9 +107,7 @@ public List getCatsNames(List peopleList, int femaleAge) { * parametrized with Candidate in CandidateValidator. */ public List validateCandidates(List candidates) { - if (candidates == null || candidates.isEmpty()) { - return Collections.emptyList(); - } + return candidates.stream() .filter(new CandidateValidator()) .map(Candidate::getName) From 9c2b0b49a4a1f7886119a9ad605bbf718f49764e Mon Sep 17 00:00:00 2001 From: FroGitHub Date: Sun, 19 Jan 2025 12:17:44 +0200 Subject: [PATCH 4/7] correcting my code x3 --- .../java/practice/CandidateValidator.java | 15 ++++++------- src/main/java/practice/StreamPractice.java | 21 ++++++++++--------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 0313eb982..92a7d030a 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -6,18 +6,15 @@ import model.Candidate; public class CandidateValidator implements Predicate { - // National for being candidate - private static final String neededNational = "Ukrainian"; - // Minimum required age for being candidate - private static final int neededAge = 35; - // Minimum required years for being candidate - private static final int neededPeriod = 10; + private static final String requirementNationality = "Ukrainian"; + private static final int requirementAge = 35; + private static final int requirementPeriodInUkr = 10; @Override public boolean test(Candidate candidate) { if (candidate == null - || !candidate.getNationality().equals(neededNational) - || candidate.getAge() < neededAge + || !candidate.getNationality().equals(requirementNationality) + || candidate.getAge() < requirementAge || !candidate.isAllowedToVote()) { return false; } @@ -27,7 +24,7 @@ public boolean test(Candidate candidate) { .map(Integer::valueOf) .toList(); if (periodInUkraine.size() != 2 - || periodInUkraine.get(1) - periodInUkraine.get(0) < neededPeriod) { + || periodInUkraine.get(1) - periodInUkraine.get(0) < requirementPeriodInUkr) { return false; } } catch (NumberFormatException | IndexOutOfBoundsException e) { diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 5551dc419..cb1832f07 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -73,10 +73,11 @@ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { return peopleList.stream() - .filter(p -> p.getAge() >= fromAge) - .filter((p -> (p.getSex() == Person.Sex.WOMAN && p.getAge() <= femaleToAge) - || (p.getSex() == Person.Sex.MAN && p.getAge() <= maleToAge))) - .collect(Collectors.toList()); + .filter(person -> person.getAge() >= fromAge) + .filter(person -> (person.getSex() == Person.Sex.WOMAN + && person.getAge() <= femaleToAge + || person.getSex() == Person.Sex.MAN && person.getAge() <= maleToAge)) + .toList(); } /** @@ -87,14 +88,14 @@ public List getWorkablePeople(int fromAge, int femaleToAge, public List getCatsNames(List peopleList, int femaleAge) { return peopleList.stream() - .filter(p -> p.getSex() == Person.Sex.WOMAN) - .filter(p -> p.getAge() >= femaleAge) - .flatMap(c -> c.getCats().stream()) + .filter(person -> person.getSex() == Person.Sex.WOMAN) + .filter(person -> person.getAge() >= femaleAge) + .flatMap(cat -> cat.getCats().stream()) .map(Cat::getName) - .collect(Collectors.toList()); + .toList(); } - /** V - wtf MA, wrong article + /** * Your help with a election is needed. Given list of candidates, where each element * has Candidate.class type. * Check which candidates are eligible to apply for president position and return their @@ -112,6 +113,6 @@ public List validateCandidates(List candidates) { .filter(new CandidateValidator()) .map(Candidate::getName) .sorted() - .collect(Collectors.toList()); + .toList(); } } From 9a6b566d641ca3f646ec93e142cb4a69c7eec18d Mon Sep 17 00:00:00 2001 From: FroGitHub Date: Sun, 19 Jan 2025 13:28:08 +0200 Subject: [PATCH 5/7] changed the names of the constants --- src/main/java/practice/CandidateValidator.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 92a7d030a..420d41ee5 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -6,15 +6,15 @@ import model.Candidate; public class CandidateValidator implements Predicate { - private static final String requirementNationality = "Ukrainian"; - private static final int requirementAge = 35; - private static final int requirementPeriodInUkr = 10; + private static final String REQUIREMENT_NATIONALITY = "Ukrainian"; + private static final int REQUIREMENT_AGE = 35; + private static final int REQUIREMENT_PERIOD_IN_UKR = 10; @Override public boolean test(Candidate candidate) { if (candidate == null - || !candidate.getNationality().equals(requirementNationality) - || candidate.getAge() < requirementAge + || !candidate.getNationality().equals(REQUIREMENT_NATIONALITY) + || candidate.getAge() < REQUIREMENT_AGE || !candidate.isAllowedToVote()) { return false; } @@ -24,7 +24,7 @@ public boolean test(Candidate candidate) { .map(Integer::valueOf) .toList(); if (periodInUkraine.size() != 2 - || periodInUkraine.get(1) - periodInUkraine.get(0) < requirementPeriodInUkr) { + || periodInUkraine.get(1) - periodInUkraine.get(0) < REQUIREMENT_PERIOD_IN_UKR) { return false; } } catch (NumberFormatException | IndexOutOfBoundsException e) { From b37ef4efbdff29f46168c307750ef1ab84cd3586 Mon Sep 17 00:00:00 2001 From: FroGitHub Date: Sun, 19 Jan 2025 13:29:50 +0200 Subject: [PATCH 6/7] changed the names of the constants --- src/main/java/practice/CandidateValidator.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 420d41ee5..f7f8f3ed3 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -24,7 +24,8 @@ public boolean test(Candidate candidate) { .map(Integer::valueOf) .toList(); if (periodInUkraine.size() != 2 - || periodInUkraine.get(1) - periodInUkraine.get(0) < REQUIREMENT_PERIOD_IN_UKR) { + || periodInUkraine.get(1) + - periodInUkraine.get(0) < REQUIREMENT_PERIOD_IN_UKR) { return false; } } catch (NumberFormatException | IndexOutOfBoundsException e) { From be5b217b80ad3ce4739ce4b676138b206a120dfc Mon Sep 17 00:00:00 2001 From: FroGitHub Date: Mon, 20 Jan 2025 13:34:58 +0200 Subject: [PATCH 7/7] improved my solution --- .../java/practice/CandidateValidator.java | 9 ++++++--- src/main/java/practice/StreamPractice.java | 20 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index f7f8f3ed3..0c59e7f92 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -9,6 +9,7 @@ public class CandidateValidator implements Predicate { private static final String REQUIREMENT_NATIONALITY = "Ukrainian"; private static final int REQUIREMENT_AGE = 35; private static final int REQUIREMENT_PERIOD_IN_UKR = 10; + private static final String PERIOD_SEPARATOR = "-"; @Override public boolean test(Candidate candidate) { @@ -18,9 +19,10 @@ public boolean test(Candidate candidate) { || !candidate.isAllowedToVote()) { return false; } + try { - List periodInUkraine = Arrays.stream(candidate.getPeriodsInUkr().split("-")) - .map(String::trim) + List periodInUkraine = Arrays + .stream(candidate.getPeriodsInUkr().split(PERIOD_SEPARATOR)) .map(Integer::valueOf) .toList(); if (periodInUkraine.size() != 2 @@ -28,10 +30,11 @@ public boolean test(Candidate candidate) { - periodInUkraine.get(0) < REQUIREMENT_PERIOD_IN_UKR) { return false; } - } catch (NumberFormatException | IndexOutOfBoundsException e) { + } catch (NumberFormatException e) { return false; } return true; } + } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index cb1832f07..e67424e85 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -3,6 +3,7 @@ import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; +import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.IntStream; import model.Candidate; @@ -10,6 +11,9 @@ import model.Person; public class StreamPractice { + + public static final CandidateValidator candidateValidator = new CandidateValidator(); + /** * Given list of strings where each element contains 1+ numbers: * input = {"5,30,100", "0,22,7", ...} @@ -23,7 +27,7 @@ public int findMinEvenNumber(List numbers) { .flatMap(s -> Arrays.stream(s.split(","))) .map(Integer::valueOf) .filter(i -> i % 2 == 0) - .min(Integer::compareTo) + .min(Integer::compareTo) // .min() -> I got error of compilation :( .orElseThrow(() -> new RuntimeException( "Can't get min value from list: " + numbers )); @@ -54,8 +58,8 @@ public Double getOddNumsAverage(List numbers) { public List selectMenByAge(List peopleList, int fromAge, int toAge) { return peopleList.stream() - .filter(p -> p.getAge() >= fromAge && p.getAge() <= toAge) - .filter(p -> p.getSex() == Person.Sex.MAN) + .filter(person -> person.getAge() >= fromAge && person.getAge() <= toAge) + .filter(person -> person.getSex() == Person.Sex.MAN) .collect(Collectors.toList()); } @@ -72,11 +76,13 @@ public List selectMenByAge(List peopleList, int fromAge, int toA public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { + Predicate ageAndSexFilter = person -> + (person.getSex() == Person.Sex.WOMAN && person.getAge() <= femaleToAge) + || (person.getSex() == Person.Sex.MAN && person.getAge() <= maleToAge); + return peopleList.stream() .filter(person -> person.getAge() >= fromAge) - .filter(person -> (person.getSex() == Person.Sex.WOMAN - && person.getAge() <= femaleToAge - || person.getSex() == Person.Sex.MAN && person.getAge() <= maleToAge)) + .filter(ageAndSexFilter) .toList(); } @@ -110,7 +116,7 @@ public List getCatsNames(List peopleList, int femaleAge) { public List validateCandidates(List candidates) { return candidates.stream() - .filter(new CandidateValidator()) + .filter(candidateValidator) .map(Candidate::getName) .sorted() .toList();