Code: Select all
int compare(T o1, T o2)
But the example code you gave uses the functional method
Code: Select all
int compareTo(T o)
Is this an error or is there some gap in my knowledge?
Code: Select all
int compare(T o1, T o2)
Code: Select all
int compareTo(T o)
The code that it shows below this paragraph i.e. games.sort( (a, b)->a.compareTo(b)); is also correct.The sort method
A collection has no notion of order but a list does. It makes sense, therefore, that List interface has a default sort(Comparator<? super E>comparator) method which allows you to sort the elements of this list using the sorting order determined by the comparator. The java.util.Comparator interface has been around since Java 1.2 but it has been made a functional interface in Java 8. Its functional method is int compare(T o1, T o2), which compares its
two arguments and returns a negative integer, zero, or a positive integer if the first argument is less than, equal to, or greater than the second.
Code: Select all
class SomeMadeUpNameComparator implements Comparator{
public int compare(String a , String b){
return a.compareTo(b); //<---- content from the lambda expression
}
}
Users browsing this forum: No registered users and 2 guests