Skip to content

Commit

Permalink
Changing loops into streamAPI in formatDataValidators
Browse files Browse the repository at this point in the history
  • Loading branch information
Rzarcik97 committed Dec 5, 2024
1 parent dc15f62 commit c9d90b0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
16 changes: 12 additions & 4 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package practice;

import java.util.Arrays;
import java.util.function.Predicate;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import model.Candidate;

public class CandidateValidator implements Predicate<Candidate> {
Expand All @@ -15,10 +19,14 @@ public boolean test(Candidate candidate) {
}

private void periodsInUkrValidator(Candidate candidate) {
for (int i = 0; i < candidate.getPeriodsInUkr().length(); i++) {
if (Character.isLetter(candidate.getPeriodsInUkr().charAt(i))) {
throw new RuntimeException("Data contains letters");
}

if (Stream.of(candidate.getPeriodsInUkr())
.map(String::chars)
.flatMap(IntStream::boxed)
.mapToInt(i -> i)
.mapToObj(i -> (char) i)
.anyMatch(Character::isLetter)) {
throw new RuntimeException("Wrong Data format");
}
}
//write your code here
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ public List<String> validateCandidates(List<Candidate> candidates) {
}

private void dataValidatorForFindMinEvenNumber(List<String> data) {
for (String datas : data) {
for (int i = 0; i < datas.length(); i++) {
if (Character.isLetter(datas.charAt(i))) {
throw new RuntimeException("Data contains letters");

}
}
if (data.stream()
.map(String::chars)
.flatMap(IntStream::boxed)
.mapToInt(i -> i)
.mapToObj(i -> (char) i)
.anyMatch(Character::isLetter)) {
throw new RuntimeException("Wrong Data format");
}
}
}

0 comments on commit c9d90b0

Please sign in to comment.