Given:
and
Which code fragment inserted on line 11 prints the average salary of all employees from the Bay Area?
Given:
and
Which code fragment inserted on line 11 prints the average salary of all employees from the Bay Area?
To print the average salary of all employees from the Bay Area, the correct code fragment should filter the employees based on their locality and then map their salaries to an integer stream to calculate the average. The code .filter(e -> e.getLocality().equals("Bay Area")).mapToInt(Employee::getSalary).average().getAsDouble() effectively performs these operations. Firstly, it filters the employees whose locality is 'Bay Area'. Then it maps the salary of these filtered employees to an IntStream using mapToInt. Finally, it calculates the average salary using the average method and retrieves the value with getAsDouble.
C is correct: for obtain the same result by collector can use: double averageSalary = roster.stream() .collect(Collectors.averagingInt(Employee::getSalary)); but is int average
"for obtain the same result by collector can use: double averageSalary = roster.stream() .collect(Collectors.averagingInt(Employee::getSalary));" this is for all employees it has to be just for "Bay Area"
answer : C The main operations in a Stream pipeline and the order in which they occur are : filter sorted map collect So, there are C and D after that. There is no such method as average() in Stream class. IntStream : OptionalDouble avarage()