diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 683be9f5..0313eb98 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 555520d0..5551dc41 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)