From e06901ee2e3b4a7a9bde20d4f04ace4887718d9c Mon Sep 17 00:00:00 2001 From: Danil Ponomarenko Date: Thu, 26 Oct 2023 15:58:26 +0300 Subject: [PATCH 1/5] Created first version of the program. --- .../java/practice/CandidateValidator.java | 26 +++++++++- src/main/java/practice/StreamPractice.java | 49 ++++++++++++++++--- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 8d2e56c0e..5cb87af45 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -1,5 +1,27 @@ package practice; -public class CandidateValidator { - //write your code here +import java.util.function.Predicate; +import model.Candidate; + +public class CandidateValidator implements Predicate { + private static final int MIN_YEARS_OLD = 35; + private static final int MIN_TOTAL_DURATION = 10; + private static final String VALID_NATIONALITY = "Ukrainian"; + private static final String REGEX = "-"; + private static final int SINCE_YEAR_INDEX = 0; + private static final int TO_YEAR_INDEX = 1; + + @Override + public boolean test(Candidate c) { + if (c.getAge() >= MIN_YEARS_OLD && c.isAllowedToVote() + && c.getNationality().equals(VALID_NATIONALITY)) { + String[] years = c.getPeriodsInUkr().split(REGEX); + + int totalDuration = Integer.parseInt(years[TO_YEAR_INDEX]) + - Integer.parseInt(years[SINCE_YEAR_INDEX]); + + return totalDuration >= MIN_TOTAL_DURATION; + } + return false; + } } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 57b1ca2e2..818f0e75e 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,8 +1,11 @@ package practice; -import java.util.Collections; +import java.util.Collection; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import model.Candidate; +import model.Cat; import model.Person; public class StreamPractice { @@ -14,7 +17,15 @@ public class StreamPractice { * "Can't get min value from list: < Here is our input 'numbers' >" */ public int findMinEvenNumber(List numbers) { - return 0; + if (numbers.size() == 0) { + throw new RuntimeException("Can't get min value from list:" + numbers); + } + return numbers.stream() + .map(s -> List.of(s.replace(" ", "").split(","))) + .flatMapToInt(l -> l.stream().mapToInt(Integer::parseInt)) + .filter(n -> n % 2 == 0) + .min() + .getAsInt(); } /** @@ -23,7 +34,13 @@ 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 != 0 ? numbers.get(i) - 1 : numbers.get(i)) + .boxed() + .filter(n -> n % 2 != 0) + .mapToDouble(n -> n) + .average() + .getAsDouble(); } /** @@ -35,7 +52,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 -> fromAge <= p.getAge() && p.getAge() <= toAge + && p.getSex() == Person.Sex.MAN) + .collect(Collectors.toList()); } /** @@ -50,7 +70,12 @@ 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.getSex() == Person.Sex.MAN && p.getAge() >= fromAge + && p.getAge() <= maleToAge) + || (p.getSex() == Person.Sex.WOMAN && p.getAge() >= fromAge + && p.getAge() <= femaleToAge)) + .collect(Collectors.toList()); } /** @@ -59,7 +84,12 @@ 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 && p.getAge() >= femaleAge) + .map(Person::getCats) + .flatMap(Collection::stream) + .map(Cat::getName) + .collect(Collectors.toList()); } /** @@ -75,6 +105,11 @@ public List getCatsNames(List peopleList, int femaleAge) { * parametrized with Candidate in CandidateValidator. */ public List validateCandidates(List candidates) { - return Collections.emptyList(); + CandidateValidator candidateValidator = new CandidateValidator(); + return candidates.stream() + .filter(candidateValidator) + .map(Candidate::getName) + .sorted() + .collect(Collectors.toList()); } } From 031e29eea680a89e9a77fb26837037e49f8dda07 Mon Sep 17 00:00:00 2001 From: Danil Ponomarenko Date: Fri, 27 Oct 2023 04:45:21 +0300 Subject: [PATCH 2/5] Student review changes: 1. Renamed parameter name in CandidateValidator test(); 2. Optimized findMinEvenNumber(): a) added constants for RuntimeException and REGEX; b) Removed useless replace(); c) optimized stream with flatMap() and orElseThrow(); 3. Optimized selectNenByAge() method; --- src/main/java/practice/CandidateValidator.java | 8 ++++---- src/main/java/practice/StreamPractice.java | 15 ++++++++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 5cb87af45..7df6a9fe6 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -12,10 +12,10 @@ public class CandidateValidator implements Predicate { private static final int TO_YEAR_INDEX = 1; @Override - public boolean test(Candidate c) { - if (c.getAge() >= MIN_YEARS_OLD && c.isAllowedToVote() - && c.getNationality().equals(VALID_NATIONALITY)) { - String[] years = c.getPeriodsInUkr().split(REGEX); + public boolean test(Candidate candidate) { + if (candidate.getAge() >= MIN_YEARS_OLD && candidate.isAllowedToVote() + && candidate.getNationality().equals(VALID_NATIONALITY)) { + String[] years = candidate.getPeriodsInUkr().split(REGEX); int totalDuration = Integer.parseInt(years[TO_YEAR_INDEX]) - Integer.parseInt(years[SINCE_YEAR_INDEX]); diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 818f0e75e..21da26334 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -9,6 +9,9 @@ import model.Person; public class StreamPractice { + private static final String EXCEPTION_MESSAGE = "Can't get min value from list: "; + private static final String REGEX = ","; + /** * Given list of strings where each element contains 1+ numbers: * input = {"5,30,100", "0,22,7", ...} @@ -17,15 +20,13 @@ public class StreamPractice { * "Can't get min value from list: < Here is our input 'numbers' >" */ public int findMinEvenNumber(List numbers) { - if (numbers.size() == 0) { - throw new RuntimeException("Can't get min value from list:" + numbers); - } return numbers.stream() - .map(s -> List.of(s.replace(" ", "").split(","))) - .flatMapToInt(l -> l.stream().mapToInt(Integer::parseInt)) + .map(s -> List.of(s.split(REGEX))) + .flatMap(Collection::stream) + .mapToInt(Integer::parseInt) .filter(n -> n % 2 == 0) .min() - .getAsInt(); + .orElseThrow(() -> new RuntimeException(EXCEPTION_MESSAGE + numbers)); } /** @@ -55,7 +56,7 @@ public List selectMenByAge(List peopleList, int fromAge, int toA return peopleList.stream() .filter(p -> fromAge <= p.getAge() && p.getAge() <= toAge && p.getSex() == Person.Sex.MAN) - .collect(Collectors.toList()); + .toList(); } /** From 7ff3a0e35bc4542c78217ee9a573e39bb8c9c6d9 Mon Sep 17 00:00:00 2001 From: Danil Ponomarenko Date: Fri, 27 Oct 2023 17:40:05 +0300 Subject: [PATCH 3/5] Created constants, optimized getOddNumsAverage(), created a new method isOddNumber, created predicate for a big condition in getWorkablePeople(). --- src/main/java/practice/StreamPractice.java | 37 +++++++++++++++------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 818f0e75e..caf6a6181 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -2,6 +2,7 @@ import java.util.Collection; import java.util.List; +import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.IntStream; import model.Candidate; @@ -9,6 +10,11 @@ import model.Person; public class StreamPractice { + private static final String EXCEPTION_MIN_VALUE_MESSAGE = "Can't get min value from list: "; + private static final String TARGET = " "; + private static final String REPLACEMENT = ""; + private static final String REGEX = ","; + /** * Given list of strings where each element contains 1+ numbers: * input = {"5,30,100", "0,22,7", ...} @@ -18,12 +24,12 @@ public class StreamPractice { */ public int findMinEvenNumber(List numbers) { if (numbers.size() == 0) { - throw new RuntimeException("Can't get min value from list:" + numbers); + throw new RuntimeException(EXCEPTION_MIN_VALUE_MESSAGE + numbers); } return numbers.stream() - .map(s -> List.of(s.replace(" ", "").split(","))) - .flatMapToInt(l -> l.stream().mapToInt(Integer::parseInt)) - .filter(n -> n % 2 == 0) + .map(string -> List.of(string.replace(TARGET, REPLACEMENT).split(REGEX))) + .flatMapToInt(listString -> listString.stream().mapToInt(Integer::parseInt)) + .filter(number -> !isOddNumber(number)) .min() .getAsInt(); } @@ -35,14 +41,16 @@ public int findMinEvenNumber(List numbers) { */ public Double getOddNumsAverage(List numbers) { return IntStream.range(0, numbers.size()) - .map(i -> i % 2 != 0 ? numbers.get(i) - 1 : numbers.get(i)) - .boxed() - .filter(n -> n % 2 != 0) - .mapToDouble(n -> n) + .map(index -> isOddNumber(index) ? numbers.get(index) - 1 : numbers.get(index)) + .filter(StreamPractice::isOddNumber) .average() .getAsDouble(); } + private static boolean isOddNumber(int number) { + return number % 2 != 0; + } + /** * Given a List of `Person` instances (having `name`, `age` and `sex` fields), * for example, `Arrays.asList( new Person(«Victor», 16, Sex.MAN), @@ -70,11 +78,16 @@ public List selectMenByAge(List peopleList, int fromAge, int toA */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { + Predicate isAgeInRange = person -> { + int age = person.getAge(); + Person.Sex sex = person.getSex(); + + return (sex == Person.Sex.MAN && age >= fromAge && age <= maleToAge) + || (sex == Person.Sex.WOMAN && age >= fromAge && age <= femaleToAge); + }; + return peopleList.stream() - .filter(p -> (p.getSex() == Person.Sex.MAN && p.getAge() >= fromAge - && p.getAge() <= maleToAge) - || (p.getSex() == Person.Sex.WOMAN && p.getAge() >= fromAge - && p.getAge() <= femaleToAge)) + .filter(isAgeInRange) .collect(Collectors.toList()); } From 26fb41b78271c077cc80793675763954a3bbcc68 Mon Sep 17 00:00:00 2001 From: Danil Ponomarenko Date: Fri, 27 Oct 2023 17:48:03 +0300 Subject: [PATCH 4/5] Optimized findMinEvenNumber & getOddNumsAverage(), created a new method isOddNumber(), created predicate for a big condition in getWorkablePeople(). --- src/main/java/practice/StreamPractice.java | 32 ++++++++++++++-------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 21da26334..2a98c0c0e 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -2,6 +2,7 @@ import java.util.Collection; import java.util.List; +import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.IntStream; import model.Candidate; @@ -21,10 +22,10 @@ public class StreamPractice { */ public int findMinEvenNumber(List numbers) { return numbers.stream() - .map(s -> List.of(s.split(REGEX))) + .map(string -> List.of(string.split(REGEX))) .flatMap(Collection::stream) .mapToInt(Integer::parseInt) - .filter(n -> n % 2 == 0) + .filter(number -> !isOddNumber(number)) .min() .orElseThrow(() -> new RuntimeException(EXCEPTION_MESSAGE + numbers)); } @@ -36,14 +37,16 @@ public int findMinEvenNumber(List numbers) { */ public Double getOddNumsAverage(List numbers) { return IntStream.range(0, numbers.size()) - .map(i -> i % 2 != 0 ? numbers.get(i) - 1 : numbers.get(i)) - .boxed() - .filter(n -> n % 2 != 0) - .mapToDouble(n -> n) + .map(index -> isOddNumber(index) ? numbers.get(index) - 1 : numbers.get(index)) + .filter(StreamPractice::isOddNumber) .average() .getAsDouble(); } + private static boolean isOddNumber(int number) { + return number % 2 != 0; + } + /** * Given a List of `Person` instances (having `name`, `age` and `sex` fields), * for example, `Arrays.asList( new Person(«Victor», 16, Sex.MAN), @@ -54,8 +57,8 @@ public Double getOddNumsAverage(List numbers) { */ public List selectMenByAge(List peopleList, int fromAge, int toAge) { return peopleList.stream() - .filter(p -> fromAge <= p.getAge() && p.getAge() <= toAge - && p.getSex() == Person.Sex.MAN) + .filter(person -> fromAge <= person.getAge() && person.getAge() <= toAge + && person.getSex() == Person.Sex.MAN) .toList(); } @@ -71,11 +74,16 @@ public List selectMenByAge(List peopleList, int fromAge, int toA */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { + Predicate isAgeInRange = person -> { + int age = person.getAge(); + Person.Sex sex = person.getSex(); + + return (sex == Person.Sex.MAN && age >= fromAge && age <= maleToAge) + || (sex == Person.Sex.WOMAN && age >= fromAge && age <= femaleToAge); + }; + return peopleList.stream() - .filter(p -> (p.getSex() == Person.Sex.MAN && p.getAge() >= fromAge - && p.getAge() <= maleToAge) - || (p.getSex() == Person.Sex.WOMAN && p.getAge() >= fromAge - && p.getAge() <= femaleToAge)) + .filter(isAgeInRange) .collect(Collectors.toList()); } From 656051a122ad81dd6096ba92c8cb57bc6b52aafd Mon Sep 17 00:00:00 2001 From: Danil Ponomarenko Date: Fri, 27 Oct 2023 18:23:45 +0300 Subject: [PATCH 5/5] Minor updates --- src/main/java/practice/CandidateValidator.java | 2 +- src/main/java/practice/StreamPractice.java | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 7df6a9fe6..614166aba 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -14,7 +14,7 @@ public class CandidateValidator implements Predicate { @Override public boolean test(Candidate candidate) { if (candidate.getAge() >= MIN_YEARS_OLD && candidate.isAllowedToVote() - && candidate.getNationality().equals(VALID_NATIONALITY)) { + && VALID_NATIONALITY.equals(candidate.getNationality())) { String[] years = candidate.getPeriodsInUkr().split(REGEX); int totalDuration = Integer.parseInt(years[TO_YEAR_INDEX]) diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 2a98c0c0e..aa1378f27 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -3,7 +3,6 @@ import java.util.Collection; import java.util.List; import java.util.function.Predicate; -import java.util.stream.Collectors; import java.util.stream.IntStream; import model.Candidate; import model.Cat; @@ -84,7 +83,7 @@ public List getWorkablePeople(int fromAge, int femaleToAge, return peopleList.stream() .filter(isAgeInRange) - .collect(Collectors.toList()); + .toList(); } /** @@ -98,7 +97,7 @@ public List getCatsNames(List peopleList, int femaleAge) { .map(Person::getCats) .flatMap(Collection::stream) .map(Cat::getName) - .collect(Collectors.toList()); + .toList(); } /** @@ -119,6 +118,6 @@ public List validateCandidates(List candidates) { .filter(candidateValidator) .map(Candidate::getName) .sorted() - .collect(Collectors.toList()); + .toList(); } }