Thursday, May 5, 2022

Question 9: Given a list of numbers, square them and filter the numbers which are greater 10000 and then find average of them.( Java 8 APIs only)

You can use the map function to square the number and then filter to avoid numbers which are less than 10000.We will use average as terminating function in this case.


import java.util.Arrays; import java.util.List; import java.util.OptionalDouble; public class RemoveDuplicatesFromListMain { public static void main(String[] args) { Integer[] arr=new Integer[]{100,24,13,44,114,200,40,112}; List<Integer> list = Arrays.asList(arr); OptionalDouble average = list.stream() .mapToInt(n->n*n) .filter(n->n>10000) .average(); if(average.isPresent()) System.out.println(average.getAsDouble()); } }

output:

21846.666666666668
Don't miss the next article! 
Be the first to be notified when a new article or Kubernetes experiment is published.                            

   Share This

You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS