-
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 practice #1211
base: main
Are you sure you want to change the base?
stream practice #1211
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 solution! 🎉 Your code effectively meets the task requirements, and you've made excellent use of the Stream API and Predicate interface. While there is a minor issue with the residency period calculation in the CandidateValidator
class, it doesn't significantly impact the overall functionality. Keep up the good work, and consider refining the residency logic to handle multiple periods for future improvements. Keep coding and learning! 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
String[] splitedPeriod = c.getPeriodsInUkr().split("-"); | ||
int startYear = Integer.parseInt(splitedPeriod[0]); | ||
int endYear = Integer.parseInt(splitedPeriod[1]); | ||
|
||
return (endYear - startYear) >= PERIOD_IN_UKRAINE; |
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.
The current logic only checks if the candidate has lived in Ukraine for a continuous period of 10 years based on a single period string. However, the task description implies that the candidate should have lived in Ukraine for a total of 10 years, which may not be continuous. Consider modifying the logic to sum up all the years from multiple periods if applicable.
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! Let’s improve your solution ;)
if (c.getAge() < MIN_AGE) { | ||
return false; | ||
} | ||
if (!c.isAllowedToVote()) { | ||
return false; | ||
} | ||
if (!NATIONALITY.equals(c.getNationality())) { | ||
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.
Can you combine these conditionals?
if (!NATIONALITY.equals(c.getNationality())) { | ||
return false; | ||
} | ||
String[] splitedPeriod = c.getPeriodsInUkr().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 the separator a constant field
} | ||
|
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.
IntStream.range(0, numbers.size()) | ||
.filter(i -> i % 2 != 0) | ||
.forEach(i -> copiedList.set(i, copiedList.get(i) - 1)); | ||
return copiedList.stream() | ||
.filter(num -> num % 2 != 0) | ||
.mapToInt(num -> num) | ||
.average() |
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 think about how to simplify it and use one stream
.filter(num -> num % 2 != 0) | ||
.mapToInt(num -> num) | ||
.average() | ||
.orElseThrow(NoSuchElementException::new); |
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.
add informative message
.filter(Objects::nonNull) | ||
.filter(p -> p.getSex() == Sex.MAN) | ||
.filter(p -> p.getAge() >= fromAge && p.getAge() <= toAge) |
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 you combine conditionals?
.filter(Objects::nonNull) | ||
.filter(p -> p.getAge() >= fromAge) | ||
.filter(p -> (p.getSex() == Sex.WOMAN && p.getAge() <= femaleToAge) | ||
|| (p.getSex() == Sex.MAN && p.getAge() <= maleToAge)) | ||
.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.
the same. for complicated conditional, it is better to use Predicate
*/ | ||
public List<String> validateCandidates(List<Candidate> candidates) { | ||
return Collections.emptyList(); | ||
return candidates.stream() | ||
.filter(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.
Better make it a class-level variable
} | ||
|
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.
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 😊
|
||
public class StreamPractice { | ||
public static final String SEPARATOR = ","; |
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.
public static final String SEPARATOR = ","; | |
private static final String SEPARATOR = ","; |
No description provided.