JetBrains Academy: Streams of primitives - Kamil-Jankowski/Learning-JAVA GitHub Wiki
The average salary:
Imagine that you need to calculate the average (monthly) salary in a company where you work at this moment.
To do this, you need to implement the calcAverageSalary
method which takes a list of salaries and calculate the value.
It is guaranteed that the company has at least one employee.
import java.util.*;
import java.util.stream.Collectors;
class CalculateAverageSalary {
private static double calcAverageSalary(Collection<Integer> salaries) {
// implement the method
return salaries.stream().mapToInt(x -> x).average().orElse(0.0);
}
/* Please do not modify the code below */
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> salaries = Arrays.stream(scanner.nextLine().split("\\s+"))
.mapToInt(Integer::parseInt)
.boxed()
.collect(Collectors.toList());
System.out.println(calcAverageSalary(salaries));
}
}