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

Add implementation for the Stream API Practice task #910

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

Conversation

MinoDentori
Copy link

No description provided.

src/main/java/practice/CandidateValidator.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
src/main/java/practice/StreamPractice.java Outdated Show resolved Hide resolved
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 points need to be processed.

.flatMap(s -> Arrays.stream(s.split(",")))
.map(Integer::parseInt)
.toList();
return allNumbers

Choose a reason for hiding this comment

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

Make it one stream.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

return allNumbers
.stream()
.filter(n -> n % 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.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

.range(0, numbers.size())
.filter(i -> i % 2 != 0)
.forEach(i -> numbers.set(i, numbers.get(i) - 1));
return numbers

Choose a reason for hiding this comment

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

Make one stream for all logic here.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

.forEach(i -> numbers.set(i, numbers.get(i) - 1));
return numbers
.stream()
.filter(n -> n % 2 != 0)

Choose a reason for hiding this comment

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

Think abut avoiding n -> n % 2 != 0 code duplication.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

Comment on lines 22 to 24
List<Integer> allNumbers = numbers
.stream()
.flatMap(s -> Arrays.stream(s.split(",")))

Choose a reason for hiding this comment

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

Suggested change
List<Integer> allNumbers = numbers
.stream()
.flatMap(s -> Arrays.stream(s.split(",")))
List<Integer> allNumbers = numbers.stream()
.flatMap(s -> Arrays.stream(s.split(",")))

It would be better not place stream() in new line.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

return Collections.emptyList();
return peopleList
.stream()
.filter(person -> person.getSex().equals(Person.Sex.MAN)

Choose a reason for hiding this comment

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

Suggested change
.filter(person -> person.getSex().equals(Person.Sex.MAN)
.filter(person -> Person.Sex.MAN.equals(person.getSex())

Let's avoid NPE.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

.filter(person -> person.getSex().equals(Person.Sex.MAN)
&& person.getAge() >= fromAge
&& person.getAge() <= toAge)
.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();

Be sure to make it everywhere.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

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

Choose a reason for hiding this comment

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

getAge() 3 times.
getAge() 2 times.
Lets make variables for it.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

@Override
public boolean test(Candidate candidate) {
String[] periodInUkr = candidate.getPeriodsInUkr().split("-");
int yearsInUkr = Integer.parseInt(periodInUkr[1]) - Integer.parseInt(periodInUkr[0]);

Choose a reason for hiding this comment

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

Let's make constants for indexes here.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

return 0;
List<Integer> allNumbers = numbers
.stream()
.flatMap(s -> Arrays.stream(s.split(",")))

Choose a reason for hiding this comment

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

Let's make constant for regex.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

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

src/main/java/practice/CandidateValidator.java Outdated Show resolved Hide resolved
src/main/java/practice/StreamPractice.java Outdated Show resolved Hide resolved
@@ -14,16 +20,31 @@ public class StreamPractice {
* "Can't get min value from list: < Here is our input 'numbers' >"
*/
public int findMinEvenNumber(List<String> numbers) {
return 0;
return numbers.stream()
.flatMap(s -> Arrays.stream(s.split(STRING_SPLITTER)))

Choose a reason for hiding this comment

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

Let's dont use one-letter names.

Copy link
Author

Choose a reason for hiding this comment

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

I guess in this kind of situations it's like i in for, fixed.

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

3 participants