Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementation for the Stream API Practice task #910

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package practice;

public class CandidateValidator {
//write your code here
import java.util.function.Predicate;
import model.Candidate;

public class CandidateValidator implements Predicate<Candidate> {
private static final int REQUIRED_YEARS_IN_UKR = 10;
private static final int MINIMAL_AGE = 35;
private static final String REQUIRED_NATIONALITY = "Ukrainian";

@Override
public boolean test(Candidate candidate) {
String[] periodInUkr = candidate.getPeriodsInUkr().split("-");
int yearsInUkr = Integer.parseInt(periodInUkr[1]) - Integer.parseInt(periodInUkr[0]);
MinoDentori marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make constants for indexes here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

return yearsInUkr >= REQUIRED_YEARS_IN_UKR
&& candidate.getAge() >= MINIMAL_AGE
&& candidate.isAllowedToVote()
&& candidate.getNationality().equals(REQUIRED_NATIONALITY);
MinoDentori marked this conversation as resolved.
Show resolved Hide resolved
}
}
60 changes: 52 additions & 8 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package practice;

import java.util.Collections;
import java.util.Arrays;
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 {
Expand All @@ -14,16 +18,31 @@ public class StreamPractice {
* "Can't get min value from list: < Here is our input 'numbers' >"
*/
public int findMinEvenNumber(List<String> numbers) {
return 0;
return numbers
.stream()
.flatMap(s -> Arrays.stream(s.split(",")))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make constant for regex.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

.map(Integer::parseInt)
.filter(n -> n % 2 == 0)
.min(Integer::compareTo)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can call min() without arguments.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

.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<Integer> numbers) {
return 0D;
public static Double getOddNumsAverage(List<Integer> numbers) {
return IntStream.range(0, numbers.size())
.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;
}

/**
Expand All @@ -35,7 +54,12 @@ public Double getOddNumsAverage(List<Integer> numbers) {
* Example: select men who can be recruited to army (from 18 to 27 years old inclusively).
*/
public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toAge) {
return Collections.emptyList();
return peopleList
.stream()
.filter(person -> person.getSex().equals(Person.Sex.MAN)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.filter(person -> person.getSex().equals(Person.Sex.MAN)
.filter(person -> Person.Sex.MAN.equals(person.getSex())

Let's avoid NPE.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

&& person.getAge() >= fromAge
&& person.getAge() <= toAge)
MinoDentori marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.toList());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.collect(Collectors.toList());
.toList();

Be sure to make it everywhere.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}

/**
Expand All @@ -50,7 +74,14 @@ public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toA
*/
public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
int maleToAge, List<Person> peopleList) {
return Collections.emptyList();
return peopleList
.stream()
.filter(person -> person.getAge() >= fromAge
&& (person.getSex().equals(Person.Sex.WOMAN)
&& person.getAge() <= femaleToAge
|| person.getSex().equals(Person.Sex.MAN)
&& person.getAge() <= maleToAge))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getAge() 3 times.
getAge() 2 times.
Lets make variables for it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

.collect(Collectors.toList());
}

/**
Expand All @@ -59,7 +90,14 @@ public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
* return the names of all cats whose owners are women from `femaleAge` years old inclusively.
*/
public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
return Collections.emptyList();
return peopleList
.stream()
.filter(person -> person.getSex().equals(Person.Sex.WOMAN)
MinoDentori marked this conversation as resolved.
Show resolved Hide resolved
&& person.getAge() >= femaleAge)
.map(Person::getCats)
.flatMap(Collection::stream)
.map(Cat::getName)
.collect(Collectors.toList());
}

/**
Expand All @@ -75,6 +113,12 @@ public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
* parametrized with Candidate in CandidateValidator.
*/
public List<String> validateCandidates(List<Candidate> candidates) {
return Collections.emptyList();
CandidateValidator candidateValidator = new CandidateValidator();
MinoDentori marked this conversation as resolved.
Show resolved Hide resolved
return candidates
.stream()
.filter(candidateValidator)
.map(Candidate::getName)
.sorted()
.collect(Collectors.toList());
}
}
Loading