Skip to content

Commit

Permalink
hw-exmpl3
Browse files Browse the repository at this point in the history
  • Loading branch information
Anasteisha4 committed Jan 20, 2025
1 parent 7414da3 commit 8c75919
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,8 +33,9 @@ public int findMinEvenNumber(List<String> numbers) {
*/

public Double getOddNumsAverage(List<Integer> 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"));
Expand Down

0 comments on commit 8c75919

Please sign in to comment.