Manish Kumar
Manish Kumar

Reputation: 227

How Comparator interface is working and how calling the compare() method internally

explain in briefly, how Comparator interface is working internally, how it calling the compare() method by JVM. In the Collections.sort() method we are passing the first parameter as list of Employee object and another parameter passing our NameComparator class object which is implementing Comparator method to short the name of employee as ascending order. So I want to know, how actually compare() method is calling with the two parameter as Employee object. Because we are not calling the compare() method. And other concept, like how actually it is working internally. The below having a snippet of code.

public class Test
{
    public static void main(String[] args)
    {
    List<Employee> list = new ArrayList<Employee>();
    list.add(new Employee("Manish"));
    list.add(new Employee("Amit"));
    list.add(new Employee("Krishna"));

    // passing the list of employee obj and NameComparator class object.
    Collections.sort(list, new NameComparator());

    System.out.println(list);
    }
}

class Employee
{
    String name = null;

    public Employee(String name)
    {
    this.name = name;
    }

    @Override
    public String toString()
    {
    return this.name;
    }
}

class NameComparator implements Comparator<Employee>
{
    @Override
    public int compare(Employee o1, Employee o2)
    {
    Employee emp1 = (Employee) o1;
    Employee emp2 = (Employee) o2;
    return emp1.name.compareTo(emp2.name);
    }
}

Upvotes: 2

Views: 1005

Answers (2)

BhavO
BhavO

Reputation: 2399

From: http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html

"The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place."

Here's the code for sort:

http://www.docjar.com/html/api/java/util/Collections.java.html

Upvotes: 3

Eran
Eran

Reputation: 393791

You can just look at the implementation. For example, in Java 6, Collections.sort calls Arrays.sort, and eventually this method is called :

    private static void mergeSort(Object[] src,
              Object[] dest,
              int low, int high, int off,
              Comparator c) {
        int length = high - low;

        // Insertion sort on smallest arrays
        if (length < INSERTIONSORT_THRESHOLD) {
            for (int i=low; i<high; i++)
                // here the Comparator's compare method is called
                for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--) 
        ...
    }

Upvotes: 2

Related Questions