From c9d90b060df3e81c39d53057e0bf5b1e4c51d3b6 Mon Sep 17 00:00:00 2001 From: Karol Gajda Date: Thu, 5 Dec 2024 13:01:48 +0100 Subject: [PATCH] Changing loops into streamAPI in formatDataValidators --- src/main/java/practice/CandidateValidator.java | 16 ++++++++++++---- src/main/java/practice/StreamPractice.java | 13 +++++++------ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index ad64f0716..856c1372a 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -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 { @@ -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 diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 807eaf490..d9f33f3c8 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -111,13 +111,14 @@ public List validateCandidates(List candidates) { } private void dataValidatorForFindMinEvenNumber(List 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"); } } }