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 26 of 100

How does the compareTo() method work in Java Collections?

πŸ“• Buy this interview preparation book: 100 Java Collections questions & answers β€” PDF + EPUB for $5

The compareTo() method is a method of the Comparable interface in Java Collections, used to define the natural ordering of elements in a collection. The 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.

The compareTo() method takes a single argument of the same type as the object being compared, and compares the object to the argument based on a predefined ordering criteria. The ordering criteria can be any characteristic of the object, such as its numeric value, alphabetical order of a string, or any other logical comparison.

Here is an example of how to use the compareTo() method to define the natural ordering of objects of a custom class:

public class Person implements Comparable<Person> {
    private String name;
    private int age;
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    @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. In this case, the compareTo() method returns the difference between the age of the current object and the age of the object being compared to.

Here is an example of how to use the compareTo() method to sort a List of Person objects:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Example {
    public static void main(String[] args) {
        // create a List of Person objects
        List<Person> people = new ArrayList<>();
        people.add(new Person("Alice", 25));
        people.add(new Person("Bob", 30));
        people.add(new Person("Charlie", 20));
    
        // sort the List of Person objects based on their natural ordering
        Collections.sort(people);
    
        // print the sorted List of Person objects
        for (Person person : people) {
            System.out.println(person.getName() + " " + person.getAge());
        }
    }
}

In this example, we create a List of Person objects and add three objects with different ages to it. We then sort the List of Person objects using the Collections.sort() method, which uses the compareTo() method we defined earlier to determine the natural ordering of Person objects. Finally, we print out the sorted List of Person objects.

The output of this program would be:

Charlie 20
Alice 25
Bob 30

As you can see, the compareTo() method is used to define the natural ordering of elements in a collection, and can be used to sort collections of custom objects based on a specific ordering 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