
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What are the differences between compareTo() and compare() methods in Java?
The Comparable interface provides a compareTo() method for the ordering of objects. This ordering is called the class’s natural ordering and the compareTo() method is called its natural comparison method. The Comparator interface provides the methods for performing sorting operations. By using the Comparator interface we can do multiple sorting sequences. We can sort the objects with respect to multiple data members.
compareTo()
compareTo() method compares this object with an o1 object and returns an integer.
Syntax
public int compareTo(Object o1)
- It returns –ve number if & only if this object is less than o1.
- It returns +ve number if & only if this object is greater than o1.
- It returns 0 if & only if this object is equal to o1.
Example
import java.util.*; class Employee implements Comparable { String name; int age; Employee(String name, int age) { this.name = name; this.age = age; } //overridden compareTo method @Override public int compareTo(Object o) { return this.age - ((Employee) o).age; } } public class ComparableDemo { public static void main(String[] args) { // CREATION List list = new ArrayList<>(); //INSERTION list.add(new Employee("Krishna", 30)); list.add(new Employee("Archana", 28)); list.add(new Employee("Vineet", 25)); list.add(new Employee("Ramesh", 38)); list.add(new Employee("Alok", 28)); System.out.println("Before sorting: "); for (Employee e : list) { System.out.print("[ EMP : age = " + e.age + " ] "); } //SORTING Collections.sort(list); System.out.println("After sorting: "); for (Employee e : list) { System.out.print("[ EMP : age = " + e.age + " ] "); } } }
Output
Before sorting: [ EMP : age = 2 ] [ EMP : age = 33 ] [ EMP : age = 11 ] [ EMP : age = 34 ] [ EMP : age = 7 ] After sorting: [ EMP : age = 2 ] [ EMP : age = 7 ] [ EMP : age = 11 ] [ EMP : age = 33 ] [ EMP : age = 34 ]
compare()
compare() method compares the first object with the second object and returns an integer
Syntax
public int compare (Object o1,Object o2)
- It returns –ve number if & only if o1 is less than o2
- It returns +ve number if & only if o1 is greater than o2
- It returns 0 if & only if o1 is equal to o2
Example
import java.util.*; class Student { String name; int age, roll; Student(String name, int age, int roll) { this.name = name; this.age = age; this.roll = roll; } } class AgeComparator implements Comparator { @Override public int compare(Object o1, Object o2) { return ((Student) o1).age - ((Student) o2).age; } } class RollComparator implements Comparator { @Override public int compare(Object o1, Object o2) { return ((Student) o1).roll - ((Student) o2).roll; } } public class ComparatorDemo { public static void main(String[] args) { List list = new ArrayList<>(); list.add(new Student("Ramesh", 30, 20)); list.add(new Student("Adithya", 7, 10)); list.add(new Student("Krishna", 25, 5)); list.add(new Student("Vineet", 24, 15)); System.out.println("BEFORE SORTING"); for (Student e : list) { System.out.println("[ STU : name = " + e.name + " age = " + e.age + " roll = " + e.roll + "]"); } Collections.sort(list,new AgeComparator()); System.out.println("AFTER SORTING WITH AGE"); for (Student e : list) { System.out.println("[ STU : name = " + e.name + " age = " + e.age + " ]"); } Collections.sort(list,new RollComparator()); System.out.println("AFTER SORTING WITH ROLL"); for (Student e : list) { System.out.println("[ STU : name = " + e.name + " roll = " + e.roll + " ]"); } } }
Output
BEFORE SORTING [ STU : name = Ramesh age = 30 roll = 20 ] [ STU : name = Adithya age = 7 roll = 10 ] [ STU : name = Krishna age = 25 roll = 5 ] [ STU : name = Vineet age = 24 roll = 15 ] AFTER SORTING WITH AGE [ STU : name = Adithya age = 7 ] [ STU : name = Vineet age = 24 ] [ STU : name = Krishna age = 25 ] [ STU : name = Ramesh age = 30 ] AFTER SORTING WITH ROLL [ STU : name = Krishna roll = 5 ] [ STU : name = Adithya roll = 10 ] [ STU : name = Vineet roll = 15 ] [ STU : name = Ramesh roll = 20 ]
- Related Articles
- What are the differences between ClassNotFoundException and NoClassDefFoundError in Java?\n
- What are the differences between length and length () in Java?\n
- What are the differences between JFrame and JDialog in Java?\n
- What are the differences between findElement() and findElements() methods?
- Java String comparison, differences between ==, equals, matches, compareTo().
- what are the differences between unshift() and push() methods in javascript?
- What are the differences between a Font and a FontMetrics in Java?\n
- Differences between wait() and join() methods in Java
- String compare by compareTo() method in Java
- What are the differences between class methods and class members in C#?
- What is the difference between equals and compareTo in Java?
- What are the differences between iCloud and SecureSafe?\n
- Differences between takewhile() and dropWhile() methods in Java 9?
- Differences between orTimeout() and completeOnTimeOut() methods in Java 9?
- Differences between Optional.ifPresentOrElse() and Optional.or() methods in Java 9?

Advertisements