2

I am begineer I have a source code, I found this method

    @Override
    public int hashCode(){

        return this.name.hashCode() + 57;

    }

It works to remove duplicates from HashSet object, my question is why does 57 exist, I deleted it then code worked well, so what is the using of numbers in this method?

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
Abdelillah
  • 79
  • 1
  • 14
  • 2
    https://stackoverflow.com/questions/52619912/why-would-one-add-a-constant-to-hashcode – Sotirios Delimanolis Nov 07 '19 at 22:39
  • 3
    @SotiriosDelimanolis Multiplying by prime is one thing, here this 57 is added for no good reason. The only reason is that whoever did it, doesn't know what he is doing. That's what I think. Let me know if you think differently. – peter.petrov Nov 07 '19 at 22:46
  • I don't see how @azurefrog helped you. You indeed don't need 57, and their link says nothing about it. –  Nov 07 '19 at 22:47
  • Yes, the two links talk about multiplication to primes. Here just some number 57 is added for no good reason. – peter.petrov Nov 07 '19 at 22:48
  • 1
    @peter.petrov, please double-check SotiriosDelimanolis's link. It talks about adding the constant (so it is a duplicate). –  Nov 07 '19 at 22:48
  • @dyukha Yes man you are right, i've read that post, has no relation with what i'm talking about – Abdelillah Nov 07 '19 at 22:50
  • @dyukha Ah? Really? – peter.petrov Nov 07 '19 at 22:50
  • @peter.petrov, `I don't see why hash is initialized to 1 rather than to employeeId. In the end this simply has the effect of adding 17*31*13 to the hashCode(), which is not going to change whether two hashCode() values are equal or not.` –  Nov 07 '19 at 22:51

2 Answers2

1

in the code:

class MyClass {
   public int hashCode(){
      return this.name.hashCode() + 57;
   }
}

The question is valid because this.name.hashCode() is a well formed hashCode() implementation (we can suppose that this.name is a String).

I think the number is added because the method hashCode() come from Object. Imagine you have a Set with values of type String and MyClass. If a String instance is the same than one of the MyClass.name then adding 57 avoid that both share the same hashcode.

  • What? A `Set` doesn't have keys. Also, a (reasonable). `Set ` can't contain both `String` and `MyClass`. A `Map` has keys, but again, only of one of those types. – daniu Nov 07 '19 at 23:38
  • You are right a set cannot have key (i will correct my post). But a Map or a set can store Object – Olivier Pellier-Cuit Nov 07 '19 at 23:42
  • Sorry but this answer is incorrect. This just doesn't make any sense. Even if a Map stores both Object and MyClass instances and there's no 57 there, there would still be no problem whatsoever. – peter.petrov Nov 09 '19 at 22:29
  • There would still be no problem but it would be more efficient if hashcode is different (no need to test for equality). I don't say it's intelligent to add 57 to a string hashcode. I just try to found a motivation for it. And by the way the hashcode can be used for more that just storing in a Map – Olivier Pellier-Cuit Nov 10 '19 at 00:16
  • @peter.petrov sure. Even if all classes implemented `hashCode` as `return 1234;`, there would still be no problem whatsoever. Except performance. Which is why we try to avoid returning the same hash code as another object that is known to be different. Granted, that change has a tiny impact. But the change is tiny as well. – Holger Nov 12 '19 at 15:45
  • @Holger OK... If you say so... so be it... First time I hear I should worry about clashes in hashCodes of objects from two different types A and B?! So... OK, that was new to me, thanks... Any reference or authoritative source for this deep subtle trick? – peter.petrov Nov 12 '19 at 17:31
  • @peter.petrov I didn’t say that you have to worry about it. It should be obvious that when you do, e.g. `Set set = new HashSet<>(); set.add("hello"); set.add("hello".hashCode());`, you already have a set with two objects of different type but the same hashcode. The set can deal with it, just like it can deal with having, e.g. `"BB"` and `"Aa"` in the same map. But having different hash codes is more efficient. While mixed type sets are rare, adding a constant isn’t an expensive thing either. So it’s a tiny operation supporting more potential use cases. – Holger Nov 22 '19 at 14:37
1

ya.....it is used for a reason as Hashcode() comes from that of object class an when in a set if we store multiple objects with example 2 parameters but we want to use the equal to compare any one of the parameter ..in this case name so this.name and 57 is added so that to generate just a good Hashcode (big enough and also it would be in the range of int..and no other good reason at all)....