🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 8 )
October 20, 2025 - For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective. Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals.
🌐
Java Programming
java-programming.mooc.fi › part-10 › 2-interface-comparable
The Comparable Interface - Java Programming
In the previous section, we looked at interfaces in more general terms - let's now familiarize ourselves with one of Java's ready-made interfaces. The Comparable interface defines the compareTo method used to compare objects.
🌐
GeeksforGeeks
geeksforgeeks.org › java › comparable-interface-in-java-with-examples
Java Comparable Interface - GeeksforGeeks
Key Method: int compareTo(T obj) - Compares the current object with the specified object. ... import java.util.*; class Student implements Comparable<Student> { String name; int marks; Student(String name, int marks) { this.name = name; this.marks ...
Published   March 11, 2022
🌐
Jenkov
jenkov.com › tutorials › java-collections › comparable.html
Java Comparable
The Java Comparable interface,java.lang.Comparable, represents an object which can be compared to other objects. For instance, numbers can be compared, strings can be compared using alphabetical comparison etc.
🌐
GeeksforGeeks
geeksforgeeks.org › java › comparable-vs-comparator-in-java
Java Comparable vs Comparator - GeeksforGeeks
August 18, 2025 - Functional Interface were introduced in Java 8. It has exactly one abstract method. In Comparator<T>, the only abstract method is: int compare(T o1, T o2);
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 7 )
For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective. Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals.
🌐
W3Schools
w3schools.com › java › java_advanced_sorting.asp
Java Advanced Sorting (Comparator and Comparable)
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... In the List Sorting Chapter, you learned how to sort lists alphabetically and numerically, but what if the list has objects in it? To sort objects you need to specify a rule that decides how objects should be sorted. For example, if you have a list of cars you might want to sort them by year, the rule could be that cars with an earlier year go first. The Comparator and Comparable interfaces allow you to specify what rule is used to sort objects.
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 6)
For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective. Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals.
Find elsewhere
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › Comparable.html
Comparable (Java SE 11 & JDK 11 )
January 20, 2026 - For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective. Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals.
🌐
Codecademy
codecademy.com › docs › java › comparable
Java | Comparable | Codecademy
April 29, 2025 - The Comparable interface in Java specifies the natural ordering for objects of a custom class. It is part of the java.lang package and provides a mechanism for comparing objects of the same type.
🌐
DigitalOcean
digitalocean.com › community › tutorials › comparable-and-comparator-in-java-example
Comparable and Comparator in Java Example | DigitalOcean
August 3, 2022 - Java provides Comparable interface which should be implemented by any custom class if we want to use Arrays or Collections sorting methods. The Comparable interface has compareTo(T obj) method which is used by sorting methods, you can check any Wrapper, String or Date class to confirm this.
🌐
Java
download.java.net › java › early_access › valhalla › docs › api › java.base › java › lang › Comparable.html
Comparable (Java SE 23 & JDK 23 [build 1])
For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective. Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals.
🌐
Javatpoint
javatpoint.com › Comparable-interface-in-collection-framework
Java Comparable - javatpoint
Java Comparable interface is used to order the user-defined class objects in natural ordering.Method of Collections class for sorting list element, java comparable example.
🌐
BeginnersBook
beginnersbook.com › 2017 › 08 › comparable-interface-in-java-with-example
Comparable Interface in Java with example
Lists (and arrays) of objects that implement Comparable interface can be sorted automatically by Collections.sort (and Arrays.sort). Before we see how to sort an objects of custom objects, lets see how we can sort elements of arrays and Wrapper classes that already implements Comparable. import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo { public static void main(String[] args) { /* * Integer class implements Comparable * Interface so we can use the sort method */ int[] arr = {11, 55, 22, 0, 89}; Arrays.sort(arr); System.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › lang › Comparable.html
Comparable (Java SE 21 & JDK 21)
January 20, 2026 - For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective. Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals.
🌐
Reddit
reddit.com › r/learnjava › i need some help understanding comparable interface
r/learnjava on Reddit: I need some help understanding Comparable interface
October 17, 2017 -

I'm struggling to understand why we need to use compareTo for comparing things when I can just create a method that does the same thing. What I understand so far is that Interfaces can hold abstract methods without implementation, and once you implement that interface on a class you have to override the method from the interface and write the implementation. What I don't understand is what is significant about (implements Comparable<T>) if all I'm gonna do is override compareTo method so it returns 1 if larger, -1 if smaller or 0 if equal.

🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › Comparable.html
Comparable (Java SE 17 & JDK 17)
January 20, 2026 - For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective. Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals.
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java comparable interface
Java Comparable Interface
July 1, 2022 - Java Comparable interface is part of Collection Framework. Learn the purpose of Comparable interface and use it in different scenarios. 1. Comparable Interface 1.1. Why Implement Comparable? In Java, if we want to sort a List of elements then we can Collections.sort() method.