0

I've thought about:

codeUnique=Math.abs(film.hashCode()+((Integer)numero).hashCode()+
                    ((Integer)fornitura).hashCode();

But use the hashCode is not ok because it isn't unique, it changes in time. Variables are these:

    private int numero;
    private String film;
    private int fornitura;

Thanks!

12
  • 1
    BTW Math.abs() can return a negative value. Commented Sep 22, 2015 at 17:50
  • 1
    @anto150192 Object.hashCode() can be different between different executions of an application. A single object cannot change it's hashCode once it is added to a hash collection (for keys and elements) without corrupting it. Commented Sep 22, 2015 at 17:52
  • 1
    @anto150192 Why it has to be unique? Commented Sep 22, 2015 at 18:10
  • 1
    Unique hashCodes will still get collisions in a Hash Collection unless you have a capacity of 4 billion (i.e. a 32-bit index) This is because the hashCode has to be reduced to a reasonable size e.g. number of bits, to save space. Commented Sep 22, 2015 at 18:30
  • 1
    @PeterLawrey yes, there will be collsions, but that are managed by the hash table (when having a correct equals method). Therefore my question why it has to be unique? Commented Sep 22, 2015 at 18:49

1 Answer 1

2

Altough a hashcode don't has to be unique, the code below should give in practise practically unique result, when numero or fornitura do not get negative, which is most likely the case. There is little chance that this code will not deliver a unique result for real world data input.

If you don't want to rely on that asumptions, then you have to introduce your own unique id which you generate when the object is constructed.

For creation of hascodes. See also: Josh Bloch: Effective Java, Second Edition, Item 9

In many cases you don't need a unique id, a suitable hashcode would look as follows:

public int hashCode() {
  int result = 17;
  result = 31 * result + numero;
  result = 31 * result + fornitura;
  if (film != null) {
    result = 31 * result + film.hashCode();
  }
  return result;
}

If you need a practically unique code, unique for real world data, without going the expensive way of creating and looking up unique codes in data (-base), you could try this: It uses long instead of int.

(Remember whole life, does not need any garuantee, it's all a question of probability)

public long uniqueCode() {
      long result = 17;
      result = 31 * result + numero;
      result = 31 * result + Long.MAX_VALUE << 16 + fornitura;
      result = 31 * result + Long.MAX_VALUE << 32 + stringCode(film);
      return result;
  }

  public static long stringCode(String s) {
     if (s == null) return 0;
     long h = 0;
     int len = s.length();
     for (int i = 0; i < len; i++) {
          h = 31*h + s.charAt(i);
     }
     return h;
  }
Sign up to request clarification or add additional context in comments.

16 Comments

While this is cleaner code, I don't see how it answers the question.
Uniqueness is hard to guarentee and no guarentee you won't get collisions. vanillajava.blogspot.com/2015/09/…
If you have fornitura = -numero * 31 and film = null they will all create the same hashCode.
If you have 64K values, it is highly likely that any two values will have the same 32-bit hash.
You were talking about film names, but any random 64K values, numbers or Strings have a good chance of having the same hashCode. For the numbers you can easily calculate when you get a collision.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.