----
reverse order :
Comparator<String> ms = (a,b) -> b.compareTo(a);
b.compareTo(a)
-1 b < a
1 b > a
Comparator
int compare (T o1, T o2)
-1 01<02
Arrays.sort(towns, ms);
returns
[paris, oman, boston, bankok]
System.out.println(Arrays.binarySearch(towns,"oman",ms));
returns 1
because ms comparator sorting is the same as before (Arrays.sort(towns, ms))
Arrays.sort method is then called with the towns array and the ms comparator, sorting the array in reverse order. The resulting sorted array is ["paris", "oman", "boston", "bangkok"]. The Arrays.binarySearch method is then called with the sorted towns array, the search key "oman", and the ms comparator. Since "oman" is found at index 1 in the sorted array, the method returns 1.
However, there is a compilation error in the code because the Comparator interface is generic and requires type arguments. The correct way to define the ms comparator would be:Comparator<String> ms = (a, b) -> b.compareTo(a);
which will print 1 after compilation
However, there is a compilation error in the code because the Comparator interface is generic and requires type arguments. The correct way to define the ms comparator would be:
Comparator<String> ms = (a, b) -> b.compareTo(a);
which will print 1 after compilation
C please not that Comparator<String> ms = (a,b)->b.compareTo(a) is not going to give you the same order as Comparator<String> ms = (a,b)->a.compareTo(b) one is descending and the other is ascending
upvoted 1 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
ASPushkin
1 month, 3 weeks agod7bb0b2
6 months agoStavok
11 months, 3 weeks agoStavok
11 months, 3 weeks agoStavok
11 months, 3 weeks agoKiraguJohn
1 year ago