-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
stream-github-practice solution #924
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job! Found only one thing that must be changed
return peopleList.stream() | ||
.filter(person -> person.getAge() >= fromAge | ||
&& person.getAge() <= toAge | ||
&& person.getSex() == Person.Sex.MAN) | ||
.toList(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here you could split all statements in different filters to make it look less complicated
public List<String> validateCandidates(List<Candidate> candidates) { | ||
return Collections.emptyList(); | ||
CandidateValidator candidateValidator = new CandidateValidator(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better when you create validator on class level, not when method calls
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job, but a few comments need to be processed.
|
||
@Override | ||
public boolean test(Candidate candidate) { | ||
String[] splitCandidate = candidate.getPeriodsInUkr().split(SEPARATION_MARK); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Find better name.
int yearsInUkr = Arrays.stream(splitCandidate) | ||
.map(String::trim) | ||
.map(Integer::parseInt) | ||
.reduce((a, b) -> b - a) | ||
.orElse(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make it with substruction?
.orElse(0); | ||
return candidate.getAge() >= MIN_AGE | ||
&& candidate.isAllowedToVote() | ||
&& candidate.getNationality().equals(NATIONALITY) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&& candidate.getNationality().equals(NATIONALITY) | |
&& NATIONALITY.equals(candidate.getNationality()) |
public int findMinEvenNumber(List<String> numbers) { | ||
return 0; | ||
return numbers.stream() | ||
.map(split -> split.split(",")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make constant for comma.
.sorted() | ||
.findFirst() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.sorted() | |
.findFirst() | |
.min() |
List<Integer> filterListNumbers = IntStream.range(0, numbers.size()) | ||
.mapToObj(i -> i % 2 == 1 ? numbers.get(i) - 1 : numbers.get(i)) | ||
.toList(); | ||
return filterListNumbers.stream() | ||
.filter(num -> num % 2 == 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make it with one stream.
Dont use one-letter names.
Avoid duplication of code that checks even or odd is a number.
Predicate<Person> predicate = person -> person.getAge() >= fromAge | ||
&& person.getAge() <= toAge | ||
&& person.getSex() == Person.Sex.MAN; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make variables to avoid double getAge() method call
Pay attention to what is a better way to compare Enum values: equals() vs == ?
if (person.getAge() >= fromAge) { | ||
if (person.getSex() == Person.Sex.WOMAN && person.getAge() <= femaleToAge) { | ||
return true; | ||
} | ||
return person.getSex() == Person.Sex.MAN && person.getAge() <= maleToAge; | ||
} | ||
return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make variables to avoid double getAge() and getSex() methods calls
Pay attention to what is a better way to compare Enum values: equals() vs == ?
return Collections.emptyList(); | ||
|
||
return peopleList.stream() | ||
.filter(women -> women.getSex() == Person.Sex.WOMAN && women.getAge() >= femaleAge) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.filter(new CandidateValidator()) | ||
.map(Candidate::getName) | ||
.sorted() | ||
.collect(Collectors.toList()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.collect(Collectors.toList()); | |
.toList(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job!
No description provided.