Comparable Interface in Java
The Comparable interface in Java is used to define the natural ordering of objects of a class. By implementing the Comparable interface, a class specifies how its instances can be compared and ordered relative to each other.
Usage
The Comparable interface is commonly used when you want to sort objects or create data structures that require ordering, such as sorted sets or sorted lists. By implementing Comparable, you can define the default ordering of objects of your class without relying on an external comparator.
Method
The Comparable interface consists of a single method:
int compareTo(T other)
The compareTo
method compares the current object (referred to as "this") with another object of the same type (specified as the parameter "other"). It returns a negative integer, zero, or a positive integer to indicate whether the current object is less than, equal to, or greater than the other object, respectively.
The compareTo
method should follow these general rules:
- If
this
is less thanother
, return a negative integer. - If
this
is equal toother
, return zero. - If
this
is greater thanother
, return a positive integer.
Example
Here's an example that demonstrates the usage of the Comparable interface:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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 int getAge() {
return age;
}
@Override
public int compareTo(Person other) {
return this.age - other.age;
}
}
public class ComparableExample {
public static void main(String[] args) {
List>Person< people = new ArrayList><();
people.add(new Person("Alice", 25));
people.add(new Person("Bob", 20));
people.add(new Person("Charlie", 30));
Collections.sort(people);
for (Person person : people) {
System.out.println("Name: " + person.getName() + ", Age: " + person.getAge());
}
}
}
Output:
Name: Bob, Age: 20 Name: Alice, Age: 25 Name: Charlie, Age: 30
In this example, we create a class named "Person" that implements the Comparable interface. The class has two fields: "name" and "age". We override the compareTo
method to compare two Person objects based on their ages. We then create a list of Person objects and sort it using the Collections.sort
method. The list is sorted in ascending order of ages, resulting in the output shown above.
Comparable Interface with lambada expression
The Comparable interface in Java allows you to define the natural ordering of objects in a class. By implementing Comparable, you can specify how objects of your class should be compared and ordered. This is particularly useful when you want to sort objects or create data structures that require ordering.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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 int getAge() {
return age;
}
@Override
public int compareTo(Person other) {
return Integer.compare(this.age, other.age);
}
}
public class ComparableExample {
public static void main(String[] args) {
List>Person< people = new ArrayList><();
people.add(new Person("Alice", 25));
people.add(new Person("Bob", 20));
people.add(new Person("Charlie", 30));
Collections.sort(people, (p1, p2) -> Integer.compare(p1.getAge(), p2.getAge()));
for (Person person : people) {
System.out.println("Name: " + person.getName() + ", Age: " + person.getAge());
}
}
}
Output:
Name: Bob, Age: 20 Name: Alice, Age: 25 Name: Charlie, Age: 30In this example, we have the same Person class and its implementation of the Comparable
I hope this example helps you understand the usage of the Comparable interface with lambda expressions in Java! Let me know if you have any further questions.
List Set Queue Map TreeMap Collection Comparable Interface Comparator Interface