Comparable Interface: The Comparable interface is defined in the java.lang package and contains a single method: compareTo(). By implementing this interface, a class indicates that its instances can be compared and sorted based on a natural order. The natural order is typically defined by the class itself and is inherent to its properties or attributes.
Key Points:
- The Comparable interface is used for natural ordering.
- It is typically implemented within the class being compared.
- The compareTo() method returns a negative integer, zero, or a positive integer to represent the ordering relationship between two objects.
Example: Let's consider a simple example of a Person class implementing the Comparable interface:
javapackage thecoderevisited;
import java.util.*;
import java.io.*;
class Person implements Comparable<Person> {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Person other) {
return this.age - other.age;
}
}
public class ComparableTest {
public static void main(String args[]) {
List<Person> personList = new ArrayList<>();
personList.add(new Person("John", 25));
personList.add(new Person("Alice", 30));
personList.add(new Person("Mike", 20));
Collections.sort(personList);
for (Person person : personList) {
System.out.println(person.getName() + " - " + person.getAge());
}
}
}
The output of the above program will be:
Mike - 20 John - 25 Alice - 30
Comparator Interface: The Comparator interface is also defined in the java.util package and contains two methods: compare() and equals(). Unlike Comparable, which is typically implemented within the class being compared, the Comparator interface is designed to provide a separate comparison logic that can be used by multiple classes.
Key Points:
- The Comparator interface is used for custom ordering or sorting.
- It is typically implemented in a separate class.
- The compare() method compares two objects based on custom-defined criteria.
- Multiple Comparator implementations can be created to compare objects in different ways.
Example: Let's consider a scenario where we want to sort a list of Person objects based on their names using a custom Comparator:
javapackage thecoderevisited;
import java.util.*;
import java.io.*;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//creating Comparator based on name
class PersonNameComparator implements Comparator<Person> {
@Override
public int compare(Person p1, Person p2) {
return p1.getName().compareTo(p2.getName());
}
}
public class ComparatorTest {
public static void main(String args[]) {
List<Person> personList = new ArrayList<>();
personList.add(new Person("John", 25));
personList.add(new Person("Alice", 30));
personList.add(new Person("Mike", 20));
Comparator<Person> nameComparator = new PersonNameComparator();
Collections.sort(personList, nameComparator);
for (Person person : personList) {
System.out.println(person.getName() + " - " + person.getAge());
}
}
}
The output of the above program will be:
Alice - 30 John - 25 Mike - 20
No comments:
Post a Comment