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

Created first version of the program. #912

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ponomvrenko
Copy link

No description provided.

Copy link

@fedorovychh fedorovychh left a 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) {

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

Comment on lines 20 to 22
if (numbers.size() == 0) {
throw new RuntimeException("Can't get min value from list:" + numbers);
}

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

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

Copy link
Author

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(",")))

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

Copy link
Author

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))

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.

Comment on lines 56 to 57
.filter(p -> fromAge <= p.getAge() && p.getAge() <= toAge
&& p.getSex() == Person.Sex.MAN)

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

Copy link
Author

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());

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;
@ponomvrenko
Copy link
Author

@proxychang thanks for your review! I just change my code with your suggestions, and it becomes more elegant <3

Copy link

@mnyshenko mnyshenko left a 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

Comment on lines 24 to 28
.map(s -> List.of(s.split(REGEX)))
.flatMap(Collection::stream)
.mapToInt(Integer::parseInt)
.filter(n -> n % 2 == 0)
.min()

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

Comment on lines 37 to 45
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();
}

Choose a reason for hiding this comment

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

A few points here:

  1. Naming, one-letter naming are acceptable only when creating for-loop
  2. a few redundant actions here; please take a look at given implementation
  3. 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;
    }

Comment on lines 57 to 58
.filter(p -> fromAge <= p.getAge() && p.getAge() <= toAge
&& p.getSex() == Person.Sex.MAN)

Choose a reason for hiding this comment

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

One-letter naming

Comment on lines 75 to 78
.filter(p -> (p.getSex() == Person.Sex.MAN && p.getAge() >= fromAge
&& p.getAge() <= maleToAge)
|| (p.getSex() == Person.Sex.WOMAN && p.getAge() >= fromAge
&& p.getAge() <= femaleToAge))

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().
Copy link

@bhdnchui bhdnchui left a 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.

src/main/java/practice/CandidateValidator.java Outdated Show resolved Hide resolved
return IntStream.range(0, numbers.size())
.map(index -> isOddNumber(index) ? numbers.get(index) - 1 : numbers.get(index))
.filter(StreamPractice::isOddNumber)
.average()

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.

Copy link
Author

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?

src/main/java/practice/StreamPractice.java Outdated Show resolved Hide resolved
src/main/java/practice/StreamPractice.java Outdated Show resolved Hide resolved
src/main/java/practice/StreamPractice.java Outdated Show resolved Hide resolved
@ponomvrenko ponomvrenko requested a review from bhdnchui October 27, 2023 15:24
Copy link

@bhdnchui bhdnchui left a comment

Choose a reason for hiding this comment

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

Good job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants