Create an almost unique identifier based on a given array of numbers

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

For instance:

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

Output:

 4532464234 

- The returned number should be as unique as possible

-Detection of elements should influence the result.

- The algorithm should always return the same result from a single input array

- The algorithm should be used as quickly as possible for use in 'for' loops

The purpose of this algorithm is to create a small value that will be stored in the database and be easily comparable. Nothing is critical, so it is acceptable that some arrays of numbers return the same value, but these cases should be rare.

Can you suggest a good way to do this?

+4
source share
4 answers

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

  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 on the values ​​of the elements.


If there is a requirement that the hash values ​​are the same for all Java platforms, I think you can rely on that to be satisfied. The javadoc says that the method will return a value that will be the same as when calling List<Integer>.hashcode() in the equivalent list. And the formula for this hash code is indicated.

+7
source

Take a look at Arrays.hashCode(int[]) , it does just that.

documentation

+2
source

What you are looking for is the hash of the array.

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

See also Java API

+1
source

I also say that 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 it depends on the implementation of the JVM.

Thus, depending on your use case, you may encounter some problems (then the solution would be to use the extern hash library).

For more information, consider this SO question: Explicit Java result constant, Object.hashCode () on all JVMs / Systems?

EDIT

I just read that you want to store the values ​​in the database. In this case, I would recommend using the extern hasing library, which reliably and guaranteedly brings the same value with every call. Otherwise, you will have to intercept your entire database every time you launch the application so that it is in a consistent state.

EDIT2

Since you use only regular int , the hash value should be the same every time. As @Stephen C showed in his answer.

+1
source

Source: https://habr.com/ru/post/1502626/


All Articles