In Java 8 we have lambdas like this one
a.sort((v1, v2) -> {});
How would this functionality be implemented in Java 7?
There are 2 differences between Java 7 and 8 relevant to this question.
List
did not have a sort
method. You had to use Collections.sort
.->
) were not part of the language.The Java 7 equivalent is
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer v1, Integer v2) {
// Write what you need here.
}
});
It basically translates to this:
a.sort(new Comparator<T>(){
public int compare(T v1, T v2){
return 0;
}
});
Java 8 introduced the "lambda" concept which allows you to eliminate the anonymous subclass syntax (among other things). You can read more about it here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With