Skip to main content

Java To note1

 Why Compareto () should be consistent to equals () method in Java?



https://javarevisited.blogspot.com/2011/11/how-to-override-compareto-method-in.html#axzz7Ij8SRkrZ

https://www.researchgate.net/post/Why-must-equals-method-be-compatible-with-compareTo-in-java

https://www.java67.com/2013/04/example-of-overriding-equals-hashcode-compareTo-java-method.html

https://stackoverflow.com/questions/12123960/what-does-comparison-being-consistent-with-equals-mean-what-can-possibly-happe

https://www.learnitweb.com/java-7/java7-tutorial/comparable-in-java/

https://www.learnitweb.com/java-7/java7-tutorial/comparator-in-java/






https://coderanch.com/t/261726/certification/compareTo-equals-methods-consistency  //g

https://softwareengineering.stackexchange.com/questions/283030/legitimate-cases-of-having-equals-behaving-inconsistently-with-compareto //g

https://dzone.com/articles/the-hidden-contract-between-equals-and-comparable  //g

https://looksok.wordpress.com/2016/05/01/java-equals-vs-compareto-contract/



https://www.appmarq.com/public/security,7556,Avoid-instanceof-in-Methods-that-override-or-implement-Objectequals-ComparablecompareTo


https://courses.cs.washington.edu/courses/cse331/11au/sections/CSE331_section2_handout_equals_comparable.pdf


https://stackoverflow.com/questions/44122341/do-i-need-to-override-also-hashcode-and-equals-methods-with-compareto-method


=>We know when we override the equals method in Java, we always have to override hashcode

-=------------------------------------------------------------

https://www.baeldung.com/java-comparator-comparable


rules for overriding compareTo():

https://javarevisited.blogspot.com/2011/11/how-to-override-compareto-method-in.html

https://www.geeksforgeeks.org/how-to-override-compareto-method-in-java/ //note the int comparing

https://www.baeldung.com/java-compareto //Guide to Implementing the compareTo Method

https://coderanch.com/t/696129/java/overriding-compareTo-compare-integers //throwing NullPointerException

https://stackoverflow.com/questions/23414984/java-overriding-compareto-method-for-arrays-sort

https://stackoverflow.com/questions/1551235/compareto-vs-equals

https://java-journal.blogspot.com/2011/01/comparable-and-comparator-part-4.html



Rules for overriding compare():

https://java-journal.blogspot.com/2011/01/comparable-and-comparator-part-4.html


Why comparator has equals method?:

https://stackoverflow.com/questions/11013850/why-does-comparator-declare-equals


=>It is generally a good idea for comparators to also implement java.io.Serializable[from comparator docs]


Why it is not necessary to override equals() of comparator interface when implementing Comparator?:

https://coderanch.com/t/503229/java/override-comparator-equals-method-overrided //g; Object class has definition of equals().

https://stackoverflow.com/questions/11758006/why-is-equals-not-mandatory-to-implement-in-java-util-comparator


https://stackoverflow.com/questions/70822969/overriding-of-equals-from-comparator-interface



Equals() consistency with compare():

my Q:

https://stackoverflow.com/questions/70822904/equals-consistency-with-compare-of-comparator-interface

https://stackoverflow.com/questions/45397657/comparator-equals-method-functionlity

-=------------------------------------------------------------------------------

equals():

https://perso.ensta-paris.fr/~diam/java/online/notes-java/data/expressions/22compareobjects.html  

If you override equals, you should also override hashCode()

Shouldn't .equals and .compareTo produce same result?

The general advice is that if a.equals(b) is true, then a.compareTo(b) == 0 should also be true.


https://stackoverflow.com/questions/1551235/compareto-vs-equals

https://stackoverflow.com/questions/45397657/comparator-equals-method-functionlity

https://stackoverflow.com/questions/7520432/what-is-the-difference-between-and-equals-in-java



Rules for overriding equals():

https://www.sitepoint.com/implement-javas-equals-method-correctly/  //g;REREAD;subclass  in getclass() section

https://cs-fundamentals.com/tech-interview/java/how-to-override-equals-method

https://medium.com/codelog/5-rules-for-overriding-equal-method-effective-java-notes-16168981374  //also equals() and inheritance

https://www.geeksforgeeks.org/overriding-equals-method-in-java/

https://stackoverflow.com/questions/8180430/how-to-override-equals-method-in-java

Effective java -item10

==

instance of

null check

cast it

logic of equal upon casting



=>equals() in Object does comparision based on ==:

https://www.javainuse.com/java/javaEquals




equals() and subclasses?



use of getClass() or instance of?:

https://www.java67.com/2021/08/java-difference-between-getclass-and.html

https://stackoverflow.com/questions/4989818/instanceof-vs-getclass  //usage

https://medium.com/@akash746/instanceof-vs-getclass-in-equals-method-in-java-3e90ec60a9cc

https://jqno.nl/equalsverifier/manual/instanceof-or-getclass/:

Note, with instanceof you don’t need an explicit null check, because instanceof already does that for you.)

...

The reason is that Josh Bloch, in his Effective Java, recommends using instanceof over getClass() because instanceof plays nicer with the Liskov substitution principle(TODO).

https://medium.com/codelog/5-rules-for-overriding-equal-method-effective-java-notes-16168981374  //also equals() and inheritance

https://www.reddit.com/r/learnjava/comments/ij79ri/using_instanceof_vs_getclass_in_equals_method/


https://www.sitepoint.com/how-to-implement-javas-hashcode-correctly/

https://www.baeldung.com/java-equals-hashcode-contracts

https://howtodoinjava.com/java/basics/java-hashcode-equals-methods/

https://www.geeksforgeeks.org/equals-hashcode-methods-java/

https://javarevisited.blogspot.com/2011/02/how-to-write-equals-method-in-java.html //Overriding equals() and hashCode() method in Java and Hibernate

https://crunchify.com/how-to-override-equals-and-hashcode-method-in-java/

ToDO:

item10,11 from effective java 3rd edititon.

-=------------------------------------------------

=>compareTo() and compare() sorts items while equal() checks only for equality