-2

I'm trying to create an abstract class Coin, having instance variables name (a String), symbol (of type java.awt.Image), and hash (a String.) The problem is that I need to make the attribute hash not the same as the one being returned by the default hashcode() method, hence having the hash of a Coin unique.

abstract class Coin {

    private String name;
    private Image symbol; 
    private String hash;

}
sny
  • 3
  • 7
  • A hash code is not a `String`. You could always try something of the form `return name.hashCode()+symbol.hashCode()`, but it is of the essence of hash codes that they are *not* unique. There are no [tag:generics] here. – user207421 Sep 14 '17 at 09:36
  • hash computing should be designed (coded) in the same philosophy like `equals()` - this is comparison to be equal. Hash can be duplicate - equals() tell the true – Jacek Cz Sep 14 '17 at 09:43
  • https://stackoverflow.com/questions/113511/best-implementation-for-hashcode-method - Check this out. Explains a good methodology for creating your own hash – aksappy Sep 14 '17 at 09:44
  • @EJP Yes you are right hash code is not a String, however in this specific assignment I'm required to make the hash a String and have the hash of a Coin unique. Well I might have interpreted wrongly the implementation of the assignment, but I would love to see the way you would implement it. (ps sorry for the sloppy code but I'm a beginner) – sny Sep 14 '17 at 09:45
  • assignment 'unique hash' is incorrect. I dont know, this is school (poor understanding), or industry (XY problem) – Jacek Cz Sep 14 '17 at 09:49
  • For uniqueness, you need an UUID (basically a large random number), not a hash. – Mick Mnemonic Sep 14 '17 at 09:49
  • @JacekCz It's a university assignment and the teacher mentioned that "the hash of a Coin is unique." I just didn't know how to implement it, because he can also refer to the class name which is Coin. – sny Sep 14 '17 at 09:54
  • You need to post the background material. A hash code is *not* a `String`, whatever you were told: it is a number. – user207421 Sep 14 '17 at 10:28
  • @EJP I've added the assignment description above, if that helps. – sny Sep 14 '17 at 11:58

2 Answers2

0

before I move on just let you know, a hash value may never be 100% unique because of hash collision.

having said that, I assume you want a unique String for variable hash(note hashCode() function in java is different as it returns an int)

there are many hashing algorithms, the one I normally use for unification in such scenario is MD5

There is an Apache utility called DiguestUtility which make life easy.

Here is an example of the use:

DigestUtils.md5(byte[] bytes);// --> returns a string of 32 char long
DigestUtils.md5(String s);// --> returns a string of 32 char long
...

Read through the methods in the documentation to see which one suits you more.

nafas
  • 5,283
  • 3
  • 29
  • 57
-2

You can override the default hashCode() function in the following way:

 @Override
public int hashCode() {
    // Unique hashcode generating function goes here
    return hash;
}

A way could be using name.hashCode()+symbol.hashCode().

Chinmay jain
  • 979
  • 1
  • 9
  • 20
  • 1
    mathematics say, such hash has poor statistic parameters - but technically is OK – Jacek Cz Sep 14 '17 at 09:44
  • generally, hashCode() method is very poor for uniquness the collision is very frequent. – nafas Sep 14 '17 at 09:55
  • @nafas Rubbish. It depends *entirely* on how the method is written. You cannot make blanket statements like that about a mere method signature. – user207421 Sep 14 '17 at 10:29
  • @EJP hashCode() returns an int so the number of combinations is bound to 32bits. even MD5 (supposed to be insecure nowadays) got 128bits, – nafas Sep 14 '17 at 11:41