-
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
Methods with lambda are implemented #1209
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.
Great job on implementing the task requirements effectively! 🎉 Your use of Java Streams and Predicate implementation is spot on. While there are a few areas for improvement, such as optimizing the getOddNumsAverage
method with IntStream
and ensuring exception messages match the task format, these are minor and don't impact the core functionality. Also, consider adding validation for the years
list to enhance robustness. Keep up the excellent work and continue refining your skills! 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
List<Integer> years = Arrays.stream(candidate.getPeriodsInUkr().split("-")) | ||
.map(Integer::parseInt) | ||
.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.
Consider adding validation to ensure that the years
list contains exactly two elements before accessing them. This will prevent potential IndexOutOfBoundsException
if the getPeriodsInUkr
method does not return a valid range.
.orElseThrow(() -> new RuntimeException("Can't get min value from list: " | ||
+ numbers)); |
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.
Ensure that the exception message format matches the task requirement: "Can't get min value from list: " + numbers.
.filter(n -> n % 2 != 0) | ||
.mapToDouble(Double::valueOf) | ||
.average() | ||
.orElseThrow(() -> new NoSuchElementException("No odd numbers found")); |
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.
Consider using IntStream
directly to filter odd numbers, which can simplify the logic and improve performance. This aligns with the checklist advice to use a single map operation where possible.
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!
return candidate.getAge() >= MINIMAL_AGE | ||
&& candidate.isAllowedToVote() | ||
&& candidate.getNationality().equals(NATIONALITY) | ||
&& (years.get(1) - years.get(0)) >= MINIMAL_YEARS_IN_UA; |
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.
you can make indexes constants too
.filter(VALIDATOR) | ||
.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(); |
it's very common, that's why the introduced .toList(), fix everywhere
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.
Ok. The answer why I made code like this : On the Mate academy platform task it was impossible to use just toList() construction.
if (person.getSex() == Person.Sex.MAN) { | ||
return person.getAge() <= maleToAge; | ||
} else { | ||
return person.getAge() <= femaleToAge; | ||
} |
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.
use ternary operator to streamline this
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 slightly improve your solution :)
.mapToDouble(i -> { | ||
int value = numbers.get(i); | ||
return i % 2 != 0 ? value - 1 : value; | ||
}) | ||
.filter(n -> n % 2 != 0.0) | ||
.average() | ||
.getAsDouble(); |
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.
.mapToDouble(i -> { | |
int value = numbers.get(i); | |
return i % 2 != 0 ? value - 1 : value; | |
}) | |
.filter(n -> n % 2 != 0.0) | |
.average() | |
.getAsDouble(); | |
.map(i -> i % 2 == 1 ? numbers.get(i) - 1 : numbers.get(i)) | |
.filter(n -> n % 2 == 1) | |
.average() | |
.orElseThrow(); |
No description provided.