1z0-819 Exam QuestionsBrowse all questions from this exam

1z0-819 Exam - Question 135


Given:

Which statement will refactor line 2 to use a lambda expression?

Show Answer
Correct Answer: AD

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.

Discussion

5 comments
Sign in to comment
ASPushkin
Apr 21, 2024

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()

duydnOption: A
Nov 21, 2023

If 1st line is List<String> -> A will be correct answer.

SampsOption: A
Jan 3, 2024

A is the most correct but it gives a compilation error as a result of ';'

d7bb0b2Option: A
Jan 9, 2024

no answer correct list.of is inmutable. if that is corrected then: A and D are correct (correction sintax for ;)

aruni_mishraOption: D
Jul 1, 2024

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); });