Concept
What is the difference between Comparable and Comparator in Java?
ComputerScienceOne / Comparators
"Each of Java’s standard types implements something similar called the Comparable interface, which specifies a compareTo() method with the same basic contract. Strings use lexicographic ordering, while numeric types such as Integer and Double use compareTo() methods that order elements in ascending order. Java refers to these built-in orderings as natural orderings. For user-defined classes, the ordering must be specified. A class can implement Comparable<T>, but doing so locks the class into one natural ordering. A Comparator is more flexible because different orderings can be defined, including descending order, even for wrapper classes that already have a natural ordering."
Related Ideas
- What is a comparator function and how does it determine element order?ComputerScienceOne · Comparator Functions
- How can Comparator order Student objects by different fields?ComputerScienceOne · Comparators
- How do you implement a comparator for integers in C?ComputerScienceOne · Comparator Functions
- How can comparator functions order Student structures?ComputerScienceOne · Comparator Functions
- What are libraries and comparators?ComputerScienceOne · Comparison & Summary . . . . . . . . . . . . . . . . . . . . . . .