Given the code fragment: List<String> empDetails = Arrays.asList("100, Robin, HR", "200, Mary, AdminServices", "101, Peter, HR"); empDetails.stream() .filter(s-> s.contains("1")) .sorted() .forEach(System.out::println); //line n1 What is the result?
Click on the arrows to vote for the correct answer
A. B. C. D.A.
The code fragment creates a list of employee details where each employee's information is a single string. It then streams the list of strings, filters the ones that contain the character "1", sorts the resulting strings, and prints them to the console using the forEach
method.
So, let's break down the steps:
Arrays.asList
method is used to create a fixed-size list containing the given employee details strings.stream
method is called on the list to create a stream of its elements.filter
method is used to keep only those strings that contain the character "1".sorted
method sorts the filtered strings.forEach
method prints each string to the console.Since the filter method keeps only the strings that contain "1", only the first and third employee details strings ("100, Robin, HR" and "101, Peter, HR") meet this criteria, and thus only those two strings are passed on to the sorted
method.
The sorted
method then sorts these two strings in their natural order, which means that "100, Robin, HR" comes before "101, Peter, HR".
Finally, the forEach
method prints the sorted strings to the console in the order they were processed, which means "100, Robin, HR" is printed first, followed by "101, Peter, HR".
Therefore, the expected output is:
A. 100, Robin, HR 101, Peter, HR
So, the correct answer is A.