Comparator: Difference between revisions
Jump to navigation
Jump to search
(Created page with "=External= * https://docs.oracle.com/javase/10/docs/api/java/util/Comparator.html =Internal= * Java 8 Lambda Expressions...") |
|||
(10 intermediate revisions by the same user not shown) | |||
Line 8: | Line 8: | ||
=Overview= | =Overview= | ||
<syntaxhighlight lang='java'> | |||
@FunctionalInterface | |||
public interface Comparator<T> { | |||
int compare(T o1, T o2); | |||
... | |||
} | |||
</syntaxhighlight> | |||
=Comparator.comparing()= | |||
Factory methods that produces comparator lambdas, based on various criteria. | |||
<syntaxhighlight lang='java'> | |||
@FunctionalInterface | |||
public interface Comparator<T> { | |||
static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor); | |||
static <T, U> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator); | |||
} | |||
</syntaxhighlight> | |||
=Concise Sorting= | |||
Java 8 List has a sort() method that accepts a java.util.Comparator. | |||
java.util.Comparator is a [[Java_8_Lambda_Expressions#Functional_Interface|functional interface]], so we can provide a [[Java_8_Lambda_Expressions#Lambda_Expression|lambda expression]] instead. | |||
java.util.Comparator comes with static methods that build Comparator instances, so if compare apples by weight, we can write this: | |||
<syntaxhighlight lang='java'> | |||
List<Apple> apples = ... | |||
apples.sort(Comparator.comparing(a -> a.getWeight())); | |||
</syntaxhighlight> | |||
The last form can be compacted even further using [[Java_8_Lambda_Expressions#Method_Reference|method references]]: | |||
<syntaxhighlight lang='java'> | |||
List<Apple> apples = ... | |||
apples.sort(Comparator.comparing(Apple::getWeight)); | |||
</syntaxhighlight> | |||
=Composition Methods= | |||
* [https://docs.oracle.com/javase/10/docs/api/java/util/Comparator.html#reversed() reversed()] | |||
* [https://docs.oracle.com/javase/10/docs/api/java/util/Comparator.html#thenComparing(java.util.function.Function) thenComparing(Function)] |
Latest revision as of 19:38, 29 March 2018
External
Internal
Overview
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
...
}
Comparator.comparing()
Factory methods that produces comparator lambdas, based on various criteria.
@FunctionalInterface
public interface Comparator<T> {
static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor);
static <T, U> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator);
}
Concise Sorting
Java 8 List has a sort() method that accepts a java.util.Comparator.
java.util.Comparator is a functional interface, so we can provide a lambda expression instead.
java.util.Comparator comes with static methods that build Comparator instances, so if compare apples by weight, we can write this:
List<Apple> apples = ...
apples.sort(Comparator.comparing(a -> a.getWeight()));
The last form can be compacted even further using method references:
List<Apple> apples = ...
apples.sort(Comparator.comparing(Apple::getWeight));