Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is equals not mandatory to implement in java.util.Comparator?

Either in Javadoc as well as the code itself, Comparator interface defines:

 int compare(T o1, T o2);
 boolean equals(Object obj);

But then this gives no probs compilating:

 Comparator a = new Comparator() {      
     @Override public int compare(Object o1, Object o2) {
        //..
     }
 };

But this does:

 Comparator a = new Comparator() {      
     @Override public boolean equals(Object comparator) {
        //..
     }
 };

How its done for the interface for allowing us not to override method?

like image 637
Whimusical Avatar asked Aug 01 '12 11:08

Whimusical


People also ask

Why equals method in Comparator is not overridden?

The equals Method obj is the object to be tested for equality. The method returns true if obj and the invoking object are both Comparator objects and use the same ordering. Otherwise, it returns false. Overriding equals( ) is unnecessary, and most simple comparators will not do so.

Which declares a Comparator where all objects are treated as equal?

The equals() method in Comparator is provided to force a user implementing the Comparator interface to implement equals() with some additional rules and constraints in addition to the ones already applied on equals() from Object .

How does Comparator function work in Java?

Comparator , represents a component that can compare two objects so they can be sorted using sorting functionality in Java. When sorting e.g a Java List you can pass a Java Comparator to the sorting method. The Comparator is then used to compare the objects in the List during sorting.

What is Java Util Comparator?

Java Comparator is an interface for sorting Java objects. Invoked by “java. util. comparator,” Java Comparator compares two Java objects in a “compare(Object 01, Object 02)” format. Using configurable methods, Java Comparator can compare objects to return an integer based on a positive, equal or negative comparison.


1 Answers

First of all JavaDocs explain clearly that you should implements this method:

Additionally, this method can return true only if the specified object is also a comparator and it imposes the same ordering as this comparator. Thus, comp1.equals(comp2)implies that sgn(comp1.compare(o1, o2))==sgn(comp2.compare(o1, o2)) for every object reference o1 and o2.

But later:

Note that it is always safe not to override Object.equals(Object).

How is it possible not to override equals(), even though it is part of an interface? Because this method is already implemented for each and every object in Java (in Object class).

The declaration in the interface is there only to emphasise the importance of equals() with regards to Comparator by adding extra JavaDoc explanation.

BTW if your comparator is stateless you should have only one instance of it - in which case the default equal() implementation is just fine.

like image 169
Tomasz Nurkiewicz Avatar answered Oct 19 '22 16:10

Tomasz Nurkiewicz