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

Solution for jv-stream-practice #923

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

Conversation

AgroFix
Copy link

@AgroFix AgroFix commented Oct 29, 2023

No description provided.

Comment on lines 9 to 10
private static final int FROM = 0;
private static final int TO = 1;

Choose a reason for hiding this comment

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

I believe that these variables are redundant

Comment on lines 47 to 52
.filter(person ->
(person.getSex() == Person.Sex.MAN && person.getAge() >= fromAge
&& person.getAge() <= maleToAge)
|| (person.getSex() == Person.Sex.WOMAN
&& person.getAge() >= fromAge
&& person.getAge() <= femaleToAge)

Choose a reason for hiding this comment

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

can be taken out of the stream and create a predicate

@OlexVoit
Copy link

Nice job!
Good luck

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 comments need to be processed.

Comment on lines 9 to 10
private static final int FROM = 0;
private static final int TO = 1;

Choose a reason for hiding this comment

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

Let's make more descriptive names.


@Override
public boolean test(Candidate candidate) {
String [] interval = candidate.getPeriodsInUkr().split(SEPARATOR);
Copy link

@bhdnchui bhdnchui Oct 30, 2023

Choose a reason for hiding this comment

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

Try to find better name.

int endLivingInUkraine = Integer.parseInt(interval[TO]);
return candidate.getAge() >= MIN_AGE
&& candidate.isAllowedToVote()
&& candidate.getNationality().equals(NATION)

Choose a reason for hiding this comment

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

Suggested change
&& candidate.getNationality().equals(NATION)
&& NATION.equals(candidate.getNationality())

public int findMinEvenNumber(List<String> numbers) {
return 0;
return numbers.stream()
.flatMap(str -> Stream.of(str.split(",")))

Choose a reason for hiding this comment

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

Create constant for comma.

.flatMap(str -> Stream.of(str.split(",")))
.map(Integer::parseInt)
.filter(num -> num % 2 == 0)
.min(Integer::compareTo)

Choose a reason for hiding this comment

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

I think you can call min() without arguments.

Comment on lines +39 to +40
&& person.getAge() >= fromAge
&& person.getAge() <= toAge)

Choose a reason for hiding this comment

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

Let's avoid double getAge method call.

&& person.getAge() >= fromAge
&& person.getAge() <= femaleToAge)
)
.collect(Collectors.toList());

Choose a reason for hiding this comment

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

Suggested change
.collect(Collectors.toList());
.toList();

Comment on lines 48 to 52
(person.getSex() == Person.Sex.MAN && person.getAge() >= fromAge
&& person.getAge() <= maleToAge)
|| (person.getSex() == Person.Sex.WOMAN
&& person.getAge() >= fromAge
&& person.getAge() <= femaleToAge)

Choose a reason for hiding this comment

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

Let's create variables to avoid getAge and getSex method calls duplication.
Pay attention to what is a better way to compare Enum values: equals() vs == ?

return Collections.emptyList();
return peopleList.stream()
.filter(person -> person.getAge() >= femaleAge
&& person.getSex().equals(Person.Sex.WOMAN))

Choose a reason for hiding this comment

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

Suggested change
&& person.getSex().equals(Person.Sex.WOMAN))
&& Person.Sex.WOMAN.equals(person.getSex()))

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

same

Copy link

@kshuryhin kshuryhin 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, some points to consider

.getAsDouble();
}

public static boolean isOdd(int number) {

Choose a reason for hiding this comment

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

Should be private I guess

Comment on lines 48 to 54
.filter(person -> {
int age = person.getAge();
Person.Sex sex = person.getSex();
return age >= fromAge
&& (Person.Sex.MAN.equals(sex) && age <= maleToAge
|| Person.Sex.WOMAN.equals(sex) && age <= femaleToAge);
})

Choose a reason for hiding this comment

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

I guess we can create separate predicate/method for this long boolean expression, what do you think?

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