From 8c759197a3dea3181ccad0654df79a63c7f9eed5 Mon Sep 17 00:00:00 2001 From: Anastasiia Dihich Date: Mon, 20 Jan 2025 09:19:21 +0200 Subject: [PATCH] hw-exmpl3 --- src/main/java/practice/StreamPractice.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index fcaa1a42..40abacaf 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -3,6 +3,7 @@ import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; +import java.util.stream.IntStream; import model.Candidate; import model.Cat; import model.Person; @@ -32,8 +33,9 @@ public int findMinEvenNumber(List numbers) { */ public Double getOddNumsAverage(List numbers) { - return numbers.stream() - .map(n -> n % 2 == 0 ? n - 1 : n) + return IntStream.range(0, numbers.size()) + .mapToObj(i -> i % 2 != 0 ? numbers.get(i) - 1 : numbers.get(i)) + .filter(n -> n % 2 != 0) .mapToInt(Integer::intValue) .average() .orElseThrow(() -> new NoSuchElementException("No odd numbers found"));