0

How should I define a hashCode for a class having 3 byte[]?

public class Key implements Comparable<Key> {
    private static final long serialVersionUID = 1L;
    private byte[] subKey1;
    private byte[] subKey2;
    private byte[] subKey3;

    public int hashCode() {  
    }

}
w5m
  • 2,286
  • 3
  • 34
  • 46
Avinash
  • 12,851
  • 32
  • 116
  • 186
  • possible duplicate of [How to implement hashCode and equals method](http://stackoverflow.com/questions/2132334/how-to-implement-hashcode-and-equals-method) – Anders R. Bystrup Mar 22 '13 at 12:51
  • possible duplicate of [Hash Code implementation](http://stackoverflow.com/questions/113511/hash-code-implementation) – hjpotter92 Mar 23 '13 at 01:00

2 Answers2

0

Simple hashcode could be to combine the length of all the 3 byte arrays and use it as hashcode.

To generate more complex one, you can read first and last elements of each byte array, convert in to an integer value and combine them to generate hashcode.

I am suggesting you to use the value, because if first and last values are same, then both the classes are most likely ot be similar.

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
0

I would suggest you google about this or to read Joshua Bloch's book "effective java" which contains a great algorithm how to do this.

secario
  • 456
  • 4
  • 14