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 the difference between Comparable and Comparator in Java? | ComputerScienceOne | Bifalgorithm | Bifalgorithm