Given:
Which statement will refactor line 2 to use a lambda expression?
Given:
Which statement will refactor line 2 to use a lambda expression?
To refactor the given code to use a lambda expression, and assuming 'fruits' is not a raw type but a List<String>, the correct statement would be: fruits.sort((String d, String e) -> (e.compareTo(d)));. This correctly uses lambda syntax to implement the Comparator's compare method, comparing two strings in reverse order.
there is no right answer new Comparator() -> new Comparator<String>() even if List fruit is a raw type list List.of("banana", "orange", "apple", "lemon"); is an Unmodifiable List so there is java.lang.UnsupportedOperationException on fruits.sort()
Note: "fruits" is Raw use of parameterized class 'List', issue with existing code: the "new Comparator" needs to be typecast as new Comparator<String>(){}, to have a valid override of public int compare(String m, String n) {} after if, we typecast fruit list, below will work: -fruits.sort((m, n) -> n.compareTo(m)); -fruits.sort((String d, String e) -> (e.compareTo(d))); -fruits.sort((a, b) -> { return b.compareTo(a); });
no answer correct list.of is inmutable. if that is corrected then: A and D are correct (correction sintax for ;)
A is the most correct but it gives a compilation error as a result of ';'
If 1st line is List<String> -> A will be correct answer.