WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Collections · Intermediate · question 25 of 100

What is the difference between the Comparable and Comparator interfaces in Java Collections?

📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

Both Comparable and Comparator interfaces are used in Java Collections to define the order of elements in a collection. The main difference between the two is the way they define the order.

The Comparable interface is used to define the natural order of elements in a collection. It is a single method interface with the signature:

public int compareTo(T o);

where T is the type of the objects being compared. This method returns a negative integer, zero, or a positive integer if the object being compared is less than, equal to, or greater than the specified object o. The natural order defined by the Comparable interface is used by sorted collections such as TreeSet and TreeMap.

Here is an example of how to use the Comparable interface to define the natural ordering of objects of a custom class:

public class Person implements Comparable<Person> {
    private String name;
    private int age;
    
    // constructor, getters and setters omitted for brevity
    
    @Override
    public int compareTo(Person other) {
        return this.age - other.age;
    }
}

In this example, we define a custom class Person that implements the Comparable interface. We override the compareTo() method to define the natural ordering of Person objects based on their age.

The Comparator interface, on the other hand, is used to define an ordering that is different from the natural order of the elements. It is also a single method interface with the signature:

public int compare(T o1, T o2);

where o1 and o2 are the objects being compared. This method returns a negative integer, zero, or a positive integer if the first object o1 is less than, equal to, or greater than the second object o2. The Comparator interface is used to sort collections based on a different criteria than the natural order defined by the Comparable interface.

Here is an example of how to use the Comparator interface to sort objects of a custom class based on a different criteria than the natural ordering:

public class PersonComparator implements Comparator<Person> {
    @Override
    public int compare(Person p1, Person p2) {
        return p1.getName().compareTo(p2.getName());
    }
}

In this example, we define a custom class PersonComparator that implements the Comparator interface. We override the compare() method to define the ordering of Person objects based on their name.

In summary, the main difference between the Comparable and Comparator interfaces in Java Collections is that the Comparable interface is used to define the natural order of elements in a collection, while the Comparator interface is used to define an ordering that is different from the natural order. The Comparable interface is used by sorted collections by default, while the Comparator interface can be used to sort collections based on different criteria.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Collections interview — then scores it.
📞 Practice Java Collections — free 15 min
📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

All 100 Java Collections questions · All topics