Java SE 8 Programmer II — Question 73

Given the code fragment:
List<String> empDetails = Arrays.asList("100, Robin, HR", "200, Mary, AdminServices","101, Peter, HR"); empDetails.stream()
.filter(s-> s.contains("r"))
.sorted()
.forEach(System.out::println); //line n1
What is the result?

Answer options

Correct answer: D

Explanation

The code filters for strings containing 'r', which includes all the entries in the list. After filtering, the entries are sorted alphabetically, resulting in '100, Robin, HR', '200, Mary, AdminServices', and '101, Peter, HR'. The correct output is option D, as it lists all entries in the expected sorted order. Options A, B, and C do not represent the correct filtered and sorted output.