5

Given an array of numbers, I would like to create a number identifier that represents that combination as unique as possible.

For example:

int[] inputNumbers = { 543, 134, 998 };
int identifier = createIdentifier(inputNumbers);
System.out.println( identifier );

Output:

4532464234

-The returned number must be as unique as possible

-Ordering of the elements must influence the result

-The algorithm must return always the same result from the same input array

-The algorithm must be as fast as possible to be used alot in 'for' loops

The purpose of this algorithm, is to create a small value to be stored in a DB, and to be easily comparable. It is nothing critical so it's acceptable that some arrays of numbers return the same value, but that cases must be rare.

Can you suggest a good way to accomplish this?

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
Hugo Pinto
  • 211
  • 1
  • 3
  • 6

4 Answers4

8

The standard ( Java 7 ) implementation of Arrays.hashCode(int[]) has the required properties. It is implemented thus:

 2938       public static int hashCode(int a[]) {
 2939           if (a == null)
 2940               return 0;
 2941   
 2942           int result = 1;
 2943           for (int element : a)
 2944               result = 31 * result + element;
 2945   
 2946           return result;
 2947       }

As you can see, the implementation is fast, and the result depends on the order of the elements as well as the element values.


If there is a requirement that the hash values are the same across all Java platforms, I think you can rely on that being satisfied. The javadoc says that the method will return a value that is that same as you get when calling List<Integer>.hashcode() on an equivalent list. And the formula for that hashcode is specified.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Okay, this implemetation overcomes the concerns stated in my answer, because it returns the same result on every JVM. But the computing of hashes in general is still JVM depended. – mike Sep 17 '13 at 14:30
  • That is exactly what I want to do. I should realize this before. Thank you. – Hugo Pinto Sep 17 '13 at 14:39
2

Have a look at Arrays.hashCode(int[]), it is doing exactly this.

documentation

Henry
  • 42,982
  • 7
  • 68
  • 84
1

What you're looking for is the array's hash code.

int hash = Arrays.hashCode(new int[]{1, 2, 3, 4});

See also the Java API

BambooleanLogic
  • 7,530
  • 3
  • 30
  • 56
1

I also say you are looking for some kind of hash function.

I don't know how much you will rely on point 3 The algorithm must return always the same result from the same input array, but this depends on the JVM implementation.

So depending on your use case you might run into some trouble (The solution then would be to use a extern hash library).

For further information take a look at this SO question: Java, Object.hashCode() result constant across all JVMs/Systems?

EDIT

I just read you want to store the values in a DB. In that case I would recommend you to use a extern hasing library that is reliable and guaranteed to yield the same value every time it is invoked. Otherwise you would have to re-hash your whole DB every time you start your application, to have it in a consistent state.

EDIT2

Since you are using only plain ints the hash value should be the same every time. As @Stephen C showed in his answer.

Community
  • 1
  • 1
mike
  • 4,929
  • 4
  • 40
  • 80