Given the code fragment:
List
System.out.println (
//line n1
);
line n1 to enable the code to print the maximum number in the nums list?
Which code fragment must be inserted at
Given the code fragment:
List
System.out.println (
//line n1
);
line n1 to enable the code to print the maximum number in the nums list?
Which code fragment must be inserted at
To find the maximum number in the list using Java Streams, you can use the stream method max along with a Comparator. The correct code fragment to do this is nums.stream().max(Comparator.comparing(a -> a)).get(). This finds the maximum element in the stream based on natural ordering and returns it.
Answer is A
A is the correct answer import java.util.Arrays; import java.util.Comparator; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(10, 20, 8); System.out.println("Maximum number: " + nums.stream() .max(Comparator.comparing(a -> a)) .get()); } }
Answer is A. Tested.
A is correct.
A, tested