-1

Someone told me that compare() and compareTo() method of comparable and comparator interface in java using hashcode to compare string or object in java.

I do not found any relevant evidence for same.

So the main question is what these two method using internally to compare objects

Theoretically what I know about the hashcode is:

The hashcode of a Java Object is simply a number, it is 32-bit signed int, that allows an object to be managed by a hash-based data structure. We know that hash code is an unique id number allocated to an object by JVM. But actually speaking, Hash code is not an unique number for an object. If two objects are equals then these two objects should return same hash code. So we have to implement hashcode() method of a class in such way that if two objects are equals, ie compared by equal() method of that class, then those two objects must return same hash code. If you are overriding hashCode you need to override equals method also.

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • Which `compare()` and `compareTo()` method exactly are you talking about? From which class/implementation? – Progman Feb 02 '20 at 21:42
  • comparable and comparator interface – Shubham Jain Feb 02 '20 at 21:43
  • The documentation of [`String#compareTo(String)`](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/String.html#compareTo(java.lang.String)) describes the natural ordering of strings; two instances are lexicographically compared. – Slaw Feb 02 '20 at 21:46
  • These are interfaces, they don't have an implementation (unless they have a so called "default" implementation). What specific class/implementation do you mean. – Progman Feb 02 '20 at 21:46
  • 2
    I would find it very strange for an implementation of `Comparable` to use the hash code of the objects being compared, as hash code values would not be a reasonable natural order. As for a `Comparator` implementation, I suppose, if for whatever reason you wanted to sort by hash code values. – Slaw Feb 02 '20 at 21:48
  • @Progman, thanks for your reply, what do you mean by lexicographically?, class= int java.lang.String.compareTo(String anotherString) – Shubham Jain Feb 02 '20 at 21:48
  • "what do you mean by lexicographically?" – the [linked documentation](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/String.html#compareTo(java.lang.String)) explains. – Slaw Feb 02 '20 at 21:52
  • 2
    Here's the quite simple [source code](http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/String.java#l1140) for `String.compareTo` in OpenJDK 8; no `hashCode` in sight. – kaya3 Feb 02 '20 at 21:52
  • may be hashcode is different than hascode function but it has a hashCode() at line 1452... I am confuse due to same – Shubham Jain Feb 02 '20 at 21:55
  • Maybe this will help: [My answer to a different question](https://stackoverflow.com/questions/59973475/sorting-objects-with-the-same-hashcode-in-java/59975198#59975198) – Scratte Feb 02 '20 at 21:59
  • 3
    @ShubhamJain Every class has a `hashCode()` method since it is defined in the `Object` class. – Progman Feb 02 '20 at 21:59
  • 1
    "but it has a hashCode() at line 1452" – yes, but that method is not called in the `compareTo` method, thus the hash value is irrelevant to the implementation of `compareTo`. – Slaw Feb 02 '20 at 22:02

1 Answers1

-1

compare and compareTo deal with something known in Java as natural ordering. Usually this means ordering things like it's done in the real world: strings a to z (lexicographically), numbers from small to large etc. More on the topic: https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

What you might heard — to make quick and dirty hack to put MyClass into a TreeMap someone could write like

class MyClass implements Comparable<MyClass> {
    // ...
    @Override
    public int compareTo(MyClass other) {
        // Dirty hack, don't use!
        return Integer.compare(hashCode(), other.hashCode());
    }
}

While this may work, I do not recommend writing compareTo() this way.

Alexey Berezkin
  • 1,513
  • 1
  • 10
  • 18
  • 3
    My 2c caveat. Since hash code is implementation-specific you might run in some edge cases where it is not the same across different JVMs or even program runs. So if you use some persistence counting on the hash key ordering then ... good luck. – Dr Phil Feb 02 '20 at 22:12