comparator |
https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html
Compare Comparable and Comparator.
compare | Comparable | Comparator |
---|---|---|
import | java.lang.Comparable; | java.util.Comparator; |
method signature | public interface Comparable<T> { public int compareTo(T o); } |
@FunctionalInterface public interface Comparator<T> { public int compare(T o1, T o2); } |
usage | Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator. | Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don't have a natural ordering. |
consistent with equals | if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C. e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false. | if and only if Comparator c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 in a set of elements. |
Code example
OCPJP>cat Compare.java
import java.util.*;
public class Compare {
public static void main(String...args) {
List<Person> persons = new ArrayList<>();
persons.add(new Person("aa", 20));
persons.add(new Person("cc", 10));
persons.add(new Person("bb", 30));
Set<Person> persons2 = new TreeSet<>(persons);
System.out.println("TreeSet persons");
System.out.println(persons2);
Set<Person> persons3 = new HashSet<>(persons);
System.out.println("Hash persons");
System.out.println(persons3);
System.out.println("ArrayList persons");
System.out.println(persons);
System.out.println("ArrayList persons sorted");
Collections.sort(persons);
System.out.println(persons);
System.out.println("binarySearch cc, 120");
int index = Collections.binarySearch(persons, new Person("cc", 120));
System.out.println(index);
System.out.println("ArrayList persons reverse sorted");
Collections.sort(persons, Comparator.reverseOrder());
System.out.println(persons);
System.out.println("binarySearch cc, 120, undefined for descending ascending order");
index = Collections.binarySearch(persons, new Person("cc", 120));
System.out.println(index);
Comparator<Person> c = (o1, o2) -> o1.age - o2.age;
Collections.sort(persons, c);
System.out.println("ArrayList persons age sorted");
System.out.println(persons);
}
}
class Person implements Comparable<Person> {
private String name;
int age; //not private, need to be accessible in Comparator
Person(String name, int age) {
this.name = name;
this.age = age;
}
public int compareTo(Person person) {
return name.compareTo(person.name);
}
@Override
public boolean equals(Object o) {
if(!(o instanceof Person)) {
return false;
}
Person person = (Person) o;
return name == person.name;
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return name + " " + age;
}
}
OCPJP>javac Compare.java
OCPJP>java Compare
TreeSet persons
[aa 20, bb 30, cc 10]
Hash persons
[aa 20, cc 10, bb 30]
ArrayList persons
[aa 20, cc 10, bb 30]
ArrayList persons sorted
[aa 20, bb 30, cc 10]
binarySearch cc, 120
2
ArrayList persons reverse sorted
[cc 10, bb 30, aa 20]
binarySearch cc, 120, undefined for descending ascending order
-4
ArrayList persons age sorted
[cc 10, aa 20, bb 30]