-2

How to write a Java 8 program to square the list of numbers and then filter out the numbers greater than 100 and then find the average of the remaining numbers?

List<Integer> numbers = List.of(12, 9, 13, 4, 6, 2, 4, 12, 15);
numbers.stream()
                   .map(number->number*number)
                   .filter(number->number>100)

Please explain now how to find the average of the remaining numbers.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • first filter, then map. On the resulting list, add all and divide by length – Stultuske Jan 23 '23 at 07:36
  • 5
    what is wrong with using the `average()` method? See its [documentation](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/stream/IntStream.html#average()) || ( BTW what is the method `numbers()`? ) – user16320675 Jan 23 '23 at 07:39
  • Can you please provide the exact code so that i can upvote your answer. Thank you so much for your quick response.Kindly post the complete solution because on execution logic is not working. I would like to see the output of your solution – ChÃrming ßoy Jan 23 '23 at 07:39
  • @ChÃrmingßoy don't ask for custom code. We're not here to do your homework for you – Stultuske Jan 23 '23 at 07:42
  • numbers is an arrayList data member variable , not function my bad – ChÃrming ßoy Jan 23 '23 at 07:44
  • 2
    better: 1) use `List numbers`; 2) use `mapToInt()` instead of `map` - you want to work with an `IntStream`; 3) to filter **out** numbers greater than 100, that is, to *maintain* numbers less than or equal to 100, the `filter` condition must be inverted (it is the condition for the elements to be *passed* on to next step – user16320675 Jan 23 '23 at 07:47
  • Suppose i want to have two pipelines together one pipeline stream for numbers greater than 100 and one pipeline stream for less than 100 how to do that and then how to count average of remaining numbers in the same functional program using stream ? – ChÃrming ßoy Jan 23 '23 at 07:55

1 Answers1

0

With this code

class Scratch {

    public static void main(String[] args) {
        System.out.println(average(List.of()));
        System.out.println(average(List.of(0, 1, 2, 3, 4, 5)));
        System.out.println(average(List.of(9, 10)));
        System.out.println(average(List.of(10, 11, 12)));
        System.out.println(average(List.of(11, 12, 13)));
        System.out.println(average(List.of(12, 9, 13, 4, 6, 2, 4, 12, 15)));
    }

    private static double average(final List<Integer> numbers) {
        return numbers.stream()
                .mapToInt(number -> number * number)
                .filter(number -> number <= 100)
                .average()
                .orElse(0);
    }

}

You will get this output:

0.0
9.166666666666666
90.5
100.0
0.0
30.6
times29
  • 2,782
  • 2
  • 21
  • 40
  • over here we are not maintaining the "Remaining Numbers" Hence the given program output is wrong and not desired. What should be the logic written to find the remaining numbers and then find the average of the remaining numbers? – ChÃrming ßoy Jan 23 '23 at 07:51
  • 1
    @ChÃrming point 3) of my [comment](https://stackoverflow.com/questions/75206692/java-8-functional-program-filter-out-the-numbers-greater-than-100-and-then-find#comment132710423_75206692)... – user16320675 Jan 23 '23 at 07:53
  • @ChÃrmingßoy sorry, I edited my post – times29 Jan 23 '23 at 07:59
  • Can we have multiple parallel streams pipeline and filter numbers greater than 100 and less than 100 both together can we do using parallel() ? – ChÃrming ßoy Jan 23 '23 at 08:01
  • 1
    @ChÃrmingßoy yes: you can use `Collectors.partitioningBy(number -> number <= 100)` – Maurice Perry Jan 23 '23 at 08:32