diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 493b67222..d9456a4f2 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -11,7 +11,7 @@ public class CandidateValidator implements Predicate { private static final int TO_YEAR_INDEX = 1; private static final String LINE_SEPARATOR = "-"; - public boolean test (Candidate candidate) { + public boolean test(Candidate candidate) { String[] period = candidate.getPeriodsInUkr().split(LINE_SEPARATOR); int yearsInUkraine = Integer.parseInt(period[TO_YEAR_INDEX]) - Integer.parseInt(period[FROM_YEAR_INDEX]); diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 09d8b1478..b2e0ede2c 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -5,7 +5,6 @@ import java.util.NoSuchElementException; import java.util.function.Predicate; import java.util.stream.IntStream; - import model.Candidate; import model.Cat; import model.Person; @@ -19,6 +18,7 @@ public class StreamPractice { * If there is no needed data throw RuntimeException with message * "Can't get min value from list: < Here is our input 'numbers' >" */ + public int findMinEvenNumber(List numbers) { return numbers.stream() .flatMap(str -> Arrays.stream(str.split(","))) @@ -26,20 +26,22 @@ public int findMinEvenNumber(List numbers) { .filter(n -> n % 2 == 0) .mapToInt(n -> n) .min() - .orElseThrow(() -> new RuntimeException("Can`t get min value from list: " + numbers)); + .orElseThrow(() -> new RuntimeException("Can`t get min value from list: " + + numbers)); } /** * Given a List of Integer numbers, * return the average of all odd numbers from the list or throw NoSuchElementException. * But before that subtract 1 from each element on an odd position (having the odd index). */ + public Double getOddNumsAverage(List numbers) { return IntStream.range(0, numbers.size()) .map(i -> i % 2 != 0 ? numbers.get(i) - 1 : numbers.get(i)) .filter(num -> num % 2 != 0) .average() - .orElseThrow(() -> new NoSuchElementException("Can`t get average value from list : " - + numbers)); + .orElseThrow(() -> new NoSuchElementException("Can`t get average " + + "value from list : " + numbers)); } /** @@ -51,8 +53,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) { - Predicate manFromAgeToAge = p -> p.getSex() == Person.Sex.MAN && p.getAge() >= fromAge - && p.getAge() <= toAge; + Predicate manFromAgeToAge = p -> p.getSex() == Person.Sex.MAN + && p.getAge() >= fromAge + && p.getAge() <= toAge; return peopleList.stream() .filter(manFromAgeToAge) .toList(); @@ -70,8 +73,9 @@ public List selectMenByAge(List peopleList, int fromAge, int toA */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { - Predicate workablePerson = p -> p.getSex() == Person.Sex.MAN && p.getAge() >= fromAge - && p.getAge() <= maleToAge || (p.getSex().equals(Person.Sex.WOMAN) && p.getAge() >= fromAge + Predicate workablePerson = p -> p.getSex() == Person.Sex.MAN + && p.getAge() >= fromAge && p.getAge() <= maleToAge + || (p.getSex().equals(Person.Sex.WOMAN) && p.getAge() >= fromAge && p.getAge() <= femaleToAge); return peopleList.stream() .filter(workablePerson)