Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Comparator is one of the most useful as well as confusing topics in Java. Useful as it provides sorting methods for the collection objects and confusing as it sounds similar to Comparable interface in Java. Thus, I bring you this article where I will be clearing all the doubts surrounding Comparator in Java.
Let’s get started.
Java Comparator interface is used to order the objects inside a user-defined class. This interface is available in java.util package and includes two crucial methods known as compare(Object obj1, Object obj2) and equals(Object element).
Now, let’s understand the various methods of Java Comparator:
There are two methods of Comparators in Java, namely:
Methods | Description |
compare(Object obj1,Object obj 2) | Compares the first object with another |
equals(Object obj) | Compares current object with specified obj |
Below code depicts the usage of both the methods by the Java Comparator.
//Employee Data
package JavaComparator; import java.util.*; import java.lang.*; import java.io.*; class Employee { int EmpID; String name, address; public Employee(int EmpID, String name, String address) { this.EmpID = EmpID; this.name = name; this.address = address; } public String toString() { return this.EmpID + " " + this.name + " " + this.address; } } class Sortbyroll implements Comparator<Employee> { public int compare(Employee a, Employee b){ return a.EmpID - b.EmpID; } } class Sortbyname implements Comparator<Employee> { public int compare(Employee a, Employee b) { return a.name.compareTo(b.name); } } class Main { public static void main (String[] args){ ArrayList<Employee> Arr = new ArrayList<Employee>(); Arr.add(new Employee(1011, "Rajesh", "Bangalore")); Arr.add(new Employee(1031, "Ralph", "Hyderabad")); Arr.add(new Employee(1201, "Karan", "Haryana")); System.out.println("Unsorted Data"); for (int i=0; i<Arr.size(); i++) System.out.println(Arr.get(i)); Collections.sort(Arr, new Sortbyroll()); System.out.println("nSorted data according to Employee IDs"); for (int i=0; i<Arr.size(); i++) System.out.println(Arr.get(i)); Collections.sort(Arr, new Sortbyname()); System.out.println("nSorted data according to Employee name"); for (int i=0; i<Arr.size(); i++) System.out.println(Arr.get(i)); } }
Output:
Unsorted Data
1011 Rajesh Bangalore
1031 Ralph Hyderabad
1201 Karan Haryana
Sorted data according to Employee IDs
1011 Rajesh Bangalore
1031 Ralph Hyderabad
1201 Karan Haryana
Sorted data according to Employee name
1201 Karan Haryana
1011 Rajesh Bangalore
1031 Ralph Hyderabad
Let’s understand the second method of Java Comparable i.e, equals method.
Java equals() example:
package Equals; public class EqualsExample { public static void main(String equals) { System.out.println(new Eqls("Harsha", 35, 12000).equals(new Eqls("Hari",25,12000))); System.out.println(new Eqls("Karan", 44, 45000).equals(new Eqls("Karan", 44, 45000))); System.out.println(new Eqls("latha", 54, 60000).equals(new Object())); } static class Eqls{ private String name; private int age; private int Salary; public Eqls(String name, int age, int Salary) { this.name = name; this.age = age; this.Salary = Salary; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Eqls eqls= (Eqls) o; return age == eqls.age && Salary == eqls.Salary && name.equals(eqls.name); } } }
false
true
false
After understanding about Java Comparator interface, let’s move towards our next topic, i.e, Comparable vs Comparable.
Comparator | Comparable |
The Comparator is used to sort attributes of different objects. | Comparable interface is used to sort the objects with natural ordering. |
A Comparator interface compares two different class objects provided. | Comparable interface compares “this” reference with the object specified. |
A Comparator is present in the java.util package. | Comparable is present in java.lang package. |
Comparator doesn’t affect the original class | Comparable affects the original class, i.e., the actual class is modified. |
Comparator provides compare() method, equals() method to sort elements. | Comparable provides compareTo() method to sort elements. |
This brings us to the end of this article on Java comparator. If you want to know more about Java you can refer to our other Java Blogs.
If you found this article on “Comparator in Java” relevant, Check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “Comparator in Java” article and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 28th September,2024 28th September SAT&SUN (Weekend Batch) | View Details |
edureka.co