
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- What are the differences between findElement() and findElements() methods?
- What are the differences between get() and navigate() methods?
- what are the differences between unshift() and push() methods in javascript?
- What are the differences between C++ and Java?
- What are the differences between C and Java?
- What are the differences between class methods and class members in C#?
- What are the differences between Java classes and Java objects?
- What are the differences between findElement() and findElements() methods in Selenium with python?
- What are the differences between close() and quit() methods in Selenium with python?
- What are the differences between switch_to_default_content() and switch_to.parent_frame() methods in Selenium with python?
- What are the differences between current_window_handle and window_handles methods in Selenium with python?
- Java String comparison, differences between ==, equals, matches, compareTo().
- Differences between wait() and join() methods in Java
- What is the difference between equals and compareTo in Java?
- What are the differences between StackOverflowError and OutOfMemoryError in Java?
Advertisements