-
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
Created first version of the program. #912
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.
You`ve done well-written solution! I commented a few points that will do your code a little more optimized, pay attention to it please. And do not forget magical numbers and constant fields.
private static final int TO_YEAR_INDEX = 1; | ||
|
||
@Override | ||
public boolean test(Candidate c) { |
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 would recommend to change Candidate variable name to something more informational
if (numbers.size() == 0) { | ||
throw 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.
It will mcuh better to delete this if/else construction and use one of Optional methods
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.
In addition, you can make exception message constant
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 suggestion, it removes useless if-else with orElseThrow() method
throw new RuntimeException("Can't get min value from list:" + numbers); | ||
} | ||
return numbers.stream() | ||
.map(s -> List.of(s.replace(" ", "").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.
You can make regex constant. And why do you need s.replace here? It seems we can avoid using replace method
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.
Haha, honestly idk, I just imagine that we have spaces around our numbers, and we have to handle this situation. Thanks!
} | ||
return numbers.stream() | ||
.map(s -> List.of(s.replace(" ", "").split(","))) | ||
.flatMapToInt(l -> l.stream().mapToInt(Integer::parseInt)) |
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.
Yes, it works properly. But it can be simplified by using just flatmap. You don`t need to change it, but think about it.
.filter(p -> fromAge <= p.getAge() && p.getAge() <= toAge | ||
&& p.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.
It`ll better to compare getSex and Sex.VALUE by equals
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 is permissible to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.
https://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals/1750453#1750453
.map(Person::getCats) | ||
.flatMap(Collection::stream) | ||
.map(Cat::getName) | ||
.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.
Maybe you can simplify it by using .toList() instead of .collect(Collectors.toList())
1. Renamed parameter name in CandidateValidator test(); 2. Optimized findMinEvenNumber(): a) added constants for RuntimeException and REGEX; b) Removed useless replace(); c) optimized stream with flatMap() and orElseThrow(); 3. Optimized selectNenByAge() method;
@proxychang thanks for your review! I just change my code with your suggestions, and it becomes more elegant <3 |
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.
A few point to consider
.map(s -> List.of(s.split(REGEX))) | ||
.flatMap(Collection::stream) | ||
.mapToInt(Integer::parseInt) | ||
.filter(n -> n % 2 == 0) | ||
.min() |
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 to give more readable names for your parameters in lambdas. One-letter names are usually used only in for-loop declarations
public Double getOddNumsAverage(List<Integer> numbers) { | ||
return 0D; | ||
return IntStream.range(0, numbers.size()) | ||
.map(i -> i % 2 != 0 ? numbers.get(i) - 1 : numbers.get(i)) | ||
.boxed() | ||
.filter(n -> n % 2 != 0) | ||
.mapToDouble(n -> n) | ||
.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.
A few points here:
- Naming, one-letter naming are acceptable only when creating for-loop
- a few redundant actions here; please take a look at given implementation
- don't repeat yourself and extract identical parts inside methods/variables for further reuse: n -> n % 2 != 0
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(NextBiggerNumber::isOddNumber)
.average()
.getAsDouble();
}
private static boolean isOddNumber(int number) {
return number % 2 != 0;
}
.filter(p -> fromAge <= p.getAge() && p.getAge() <= toAge | ||
&& p.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.
One-letter naming
.filter(p -> (p.getSex() == Person.Sex.MAN && p.getAge() >= fromAge | ||
&& p.getAge() <= maleToAge) | ||
|| (p.getSex() == Person.Sex.WOMAN && p.getAge() >= fromAge | ||
&& p.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.
p.getAge() used twice so can be extracted to a separate variable, same with sex
…d isOddNumber, created predicate for a big condition in getWorkablePeople().
# Conflicts: # src/main/java/practice/StreamPractice.java
…od isOddNumber(), created predicate for a big condition in getWorkablePeople().
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 minor points need to consider.
return IntStream.range(0, numbers.size()) | ||
.map(index -> isOddNumber(index) ? numbers.get(index) - 1 : numbers.get(index)) | ||
.filter(StreamPractice::isOddNumber) | ||
.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.
Check if there is an average number. If there is not, throw NoSuchElementException according to the task requirements.
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.
@bhdnchui I have there getAsDouble() method that includes
if (!isPresent) { throw new NoSuchElementException("No value present"); }
Is my solution here incorrect?
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.