From 63ef2520ec724882117669ab4749736b5d08f275 Mon Sep 17 00:00:00 2001 From: AgroFix Date: Sun, 29 Oct 2023 15:47:32 +0200 Subject: [PATCH 1/4] Solution for jv-stream-practice --- .../java/practice/CandidateValidator.java | 23 +++- src/main/java/practice/StreamPractice.java | 101 ++++++++---------- 2 files changed, 68 insertions(+), 56 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 8d2e56c0e..1e04866a8 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -1,5 +1,24 @@ 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_AGE = 35; + private static final int MIN_YEARS_IN_UKRAINE = 10; + private static final int FROM = 0; + private static final int TO = 1; + private static final String NATION = "Ukrainian"; + private static final String SEPARATOR = "-"; + + @Override + public boolean test(Candidate candidate) { + String [] interval = candidate.getPeriodsInUkr().split(SEPARATOR); + int startLivingInUkraine = Integer.parseInt(interval[FROM]); + int endLivingInUkraine = Integer.parseInt(interval[TO]); + return candidate.getAge() >= MIN_AGE + && candidate.isAllowedToVote() + && candidate.getNationality().equals(NATION) + && endLivingInUkraine - startLivingInUkraine >= MIN_YEARS_IN_UKRAINE; + } } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 57b1ca2e2..ad3456559 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,80 +1,73 @@ package practice; -import java.util.Collections; +import java.util.Collection; import java.util.List; +import java.util.NoSuchElementException; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; import model.Candidate; +import model.Cat; import model.Person; public class StreamPractice { - /** - * Given list of strings where each element contains 1+ numbers: - * input = {"5,30,100", "0,22,7", ...} - * return min integer value. One more thing - we're interested in even numbers. - * If there is no needed data throw RuntimeException with message - * "Can't get min value from list: < Here is our input 'numbers' >" - */ + private static final String BAD_INPUT_MESSAGE = "No odd numbers in the list"; + private static final String MIN_VALUE_ERROR_MESSAGE = "Can't get min value from list: %s"; + public int findMinEvenNumber(List numbers) { - return 0; + return numbers.stream() + .flatMap(str -> Stream.of(str.split(","))) + .map(Integer::parseInt) + .filter(num -> num % 2 == 0) + .min(Integer::compareTo) + .orElseThrow(() -> new RuntimeException( + String.format(MIN_VALUE_ERROR_MESSAGE, 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 0D; + return IntStream.range(0, numbers.size()) + .mapToDouble(number -> number % 2 != 0 ? numbers.get(number) - 1 + : numbers.get(number)) + .filter(n -> n % 2 != 0) + .average() + .orElseThrow(() -> new NoSuchElementException(BAD_INPUT_MESSAGE)); } - /** - * Given a List of `Person` instances (having `name`, `age` and `sex` fields), - * for example, `Arrays.asList( new Person(«Victor», 16, Sex.MAN), - * new Person(«Helen», 42, Sex.WOMAN))`, - * select from the List only men whose age is from `fromAge` to `toAge` inclusively. - *

- * 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(person -> person.getSex() == Person.Sex.MAN + && person.getAge() >= fromAge + && person.getAge() <= toAge) + .toList(); } - /** - * Given a List of `Person` instances (having `name`, `age` and `sex` fields), - * for example, `Arrays.asList( new Person(«Victor», 16, Sex.MAN), - * new Person(«Helen», 42, Sex.WOMAN))`, - * select from the List only people whose age is from `fromAge` and to `maleToAge` (for men) - * or to `femaleToAge` (for women) inclusively. - *

- * Example: select people of working age - * (from 18 y.o. and to 60 y.o. for men and to 55 y.o. for women inclusively). - */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { - return Collections.emptyList(); + return peopleList.stream() + .filter(person -> + (person.getSex() == Person.Sex.MAN && person.getAge() >= fromAge + && person.getAge() <= maleToAge) + || (person.getSex() == Person.Sex.WOMAN + && person.getAge() >= fromAge + && person.getAge() <= femaleToAge) + ) + .collect(Collectors.toList()); } - /** - * Given a List of `Person` instances (having `name`, `age`, `sex` and `cats` fields, - * and each `Cat` having a `name` and `age`), - * 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(person -> person.getAge() >= femaleAge && person.getSex().equals(Person.Sex.WOMAN)) + .map(Person::getCats) + .flatMap(Collection::stream) + .map(Cat::getName) + .collect(Collectors.toList()); } - /** - * 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 - * names sorted alphabetically. - * The requirements are: person should be older than 35 years, should be allowed to vote, - * have nationality - 'Ukrainian' - * and live in Ukraine for 10 years. For the last requirement use field periodsInUkr, - * which has following view: "2002-2015" - * We want to reuse our validation in future, so let's write our own impl of Predicate - * parametrized with Candidate in CandidateValidator. - */ public List validateCandidates(List candidates) { - return Collections.emptyList(); + return candidates.stream() + .filter(new CandidateValidator()) + .map(Candidate::getName) + .sorted() + .toList(); } } From 8a0459b668059d9abc0d72ea1ca1907de03b293c Mon Sep 17 00:00:00 2001 From: AgroFix Date: Sun, 29 Oct 2023 15:49:32 +0200 Subject: [PATCH 2/4] Solution for jv-stream-practice --- src/main/java/practice/StreamPractice.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index ad3456559..18e5cfa32 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -56,7 +56,8 @@ public List getWorkablePeople(int fromAge, int femaleToAge, public List getCatsNames(List peopleList, int femaleAge) { return peopleList.stream() - .filter(person -> person.getAge() >= femaleAge && person.getSex().equals(Person.Sex.WOMAN)) + .filter(person -> person.getAge() >= femaleAge + && person.getSex().equals(Person.Sex.WOMAN)) .map(Person::getCats) .flatMap(Collection::stream) .map(Cat::getName) From 6e7a8764993a1a608ed68448c5beba0b6fe53130 Mon Sep 17 00:00:00 2001 From: AgroFix Date: Mon, 30 Oct 2023 15:45:05 +0200 Subject: [PATCH 3/4] Fixed some points --- .../java/practice/CandidateValidator.java | 12 +++--- src/main/java/practice/StreamPractice.java | 41 ++++++++++--------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 1e04866a8..f5fb9b329 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -6,19 +6,19 @@ public class CandidateValidator implements Predicate { private static final int MIN_AGE = 35; private static final int MIN_YEARS_IN_UKRAINE = 10; - private static final int FROM = 0; - private static final int TO = 1; + private static final int START_OF_PERIOD = 0; + private static final int END_OF_PERIOD = 1; private static final String NATION = "Ukrainian"; private static final String SEPARATOR = "-"; @Override public boolean test(Candidate candidate) { - String [] interval = candidate.getPeriodsInUkr().split(SEPARATOR); - int startLivingInUkraine = Integer.parseInt(interval[FROM]); - int endLivingInUkraine = Integer.parseInt(interval[TO]); + String [] livingInUkrainePeriod = candidate.getPeriodsInUkr().split(SEPARATOR); + int startLivingInUkraine = Integer.parseInt(livingInUkrainePeriod[START_OF_PERIOD]); + int endLivingInUkraine = Integer.parseInt(livingInUkrainePeriod[END_OF_PERIOD]); return candidate.getAge() >= MIN_AGE && candidate.isAllowedToVote() - && candidate.getNationality().equals(NATION) + && NATION.equals(candidate.getNationality()) && endLivingInUkraine - startLivingInUkraine >= MIN_YEARS_IN_UKRAINE; } } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 18e5cfa32..111b8c4e8 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -2,8 +2,6 @@ import java.util.Collection; import java.util.List; -import java.util.NoSuchElementException; -import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; import model.Candidate; @@ -11,31 +9,34 @@ import model.Person; public class StreamPractice { - private static final String BAD_INPUT_MESSAGE = "No odd numbers in the list"; private static final String MIN_VALUE_ERROR_MESSAGE = "Can't get min value from list: %s"; + private static final String COMMA = ","; public int findMinEvenNumber(List numbers) { return numbers.stream() - .flatMap(str -> Stream.of(str.split(","))) - .map(Integer::parseInt) + .flatMap(str -> Stream.of(str.split(COMMA))) + .mapToInt(Integer::parseInt) .filter(num -> num % 2 == 0) - .min(Integer::compareTo) + .min() .orElseThrow(() -> new RuntimeException( String.format(MIN_VALUE_ERROR_MESSAGE, numbers))); } public Double getOddNumsAverage(List numbers) { return IntStream.range(0, numbers.size()) - .mapToDouble(number -> number % 2 != 0 ? numbers.get(number) - 1 - : numbers.get(number)) - .filter(n -> n % 2 != 0) + .mapToDouble(num -> isOdd(num) ? numbers.get(num) - 1 : numbers.get(num)) + .filter(n -> isOdd((int) n)) .average() - .orElseThrow(() -> new NoSuchElementException(BAD_INPUT_MESSAGE)); + .getAsDouble(); + } + + public static boolean isOdd(int number) { + return number % 2 != 0; } public List selectMenByAge(List peopleList, int fromAge, int toAge) { return peopleList.stream() - .filter(person -> person.getSex() == Person.Sex.MAN + .filter(person -> person.getSex().equals(Person.Sex.MAN) && person.getAge() >= fromAge && person.getAge() <= toAge) .toList(); @@ -44,14 +45,14 @@ public List selectMenByAge(List peopleList, int fromAge, int toA public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { return peopleList.stream() - .filter(person -> - (person.getSex() == Person.Sex.MAN && person.getAge() >= fromAge - && person.getAge() <= maleToAge) - || (person.getSex() == Person.Sex.WOMAN - && person.getAge() >= fromAge - && person.getAge() <= femaleToAge) - ) - .collect(Collectors.toList()); + .filter(person -> { + int age = person.getAge(); + Person.Sex sex = person.getSex(); + return age >= fromAge + && (Person.Sex.MAN.equals(sex) && age <= maleToAge + || Person.Sex.WOMAN.equals(sex) && age <= femaleToAge); + }) + .toList(); } public List getCatsNames(List peopleList, int femaleAge) { @@ -61,7 +62,7 @@ public List getCatsNames(List peopleList, int femaleAge) { .map(Person::getCats) .flatMap(Collection::stream) .map(Cat::getName) - .collect(Collectors.toList()); + .toList(); } public List validateCandidates(List candidates) { From 1f8333a4c12d4beec00eab0af2b695af9a7c7dc6 Mon Sep 17 00:00:00 2001 From: AgroFix Date: Mon, 30 Oct 2023 18:26:26 +0200 Subject: [PATCH 4/4] Fixed some points --- src/main/java/practice/StreamPractice.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 111b8c4e8..7f2aec924 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.IntStream; import java.util.stream.Stream; import model.Candidate; @@ -30,7 +31,7 @@ public Double getOddNumsAverage(List numbers) { .getAsDouble(); } - public static boolean isOdd(int number) { + private static boolean isOdd(int number) { return number % 2 != 0; } @@ -44,14 +45,16 @@ public List selectMenByAge(List peopleList, int fromAge, int toA public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { + + Predicate personCheck = person -> { + int age = person.getAge(); + Person.Sex sex = person.getSex(); + return age >= fromAge + && (Person.Sex.MAN.equals(sex) && age <= maleToAge + || Person.Sex.WOMAN.equals(sex) && age <= femaleToAge); + }; return peopleList.stream() - .filter(person -> { - int age = person.getAge(); - Person.Sex sex = person.getSex(); - return age >= fromAge - && (Person.Sex.MAN.equals(sex) && age <= maleToAge - || Person.Sex.WOMAN.equals(sex) && age <= femaleToAge); - }) + .filter(personCheck) .toList(); }