Set average

Report a typo

Implement the avgOnSet method that accepts a stream of Integer elements, accumulates them to a Set, and returns the average value of numbers from the set.

Remember to use the collect operation to solve the problem.

Sample Input 1:

1 3 1 3 1

Sample Output 1:

2.0

Sample Input 2:

1 2 3 4 2

Sample Output 2:

2.5
Write a program in Java 17
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

class Main {

/**
* Returns the average value of numbers from a Set
* that is accumulated from the input stream.
*
* @param numbers the input stream of Integer elements
* @return average value of a Set of numbers
*/
public static Double avgOnSet(Stream<Integer> numbers) {
// write your code here
}

// Don't change the code below
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Stream<Integer> integerStream = Arrays.stream(scanner.nextLine()
.split("\\s+")).map(Integer::parseInt);
System.out.println(avgOnSet(integerStream));
}
}
___

Create a free account to access the full topic