Get hashcode with hashCode()

Get hashcode with hashCode()


About Hashcode

Generally, a programmer writes a lengthy for loop to compare two strings for their equality, checking each character in both the strings. It is a time consuming process and performance goes down especially when the strings are lengthy. To overcome this, Java designers introduced hash code. It is an integer number that can represent an array, a string, an object or a data structure. The hashCode() method of Arrays return a 32-bit integer number that represents an array. The number changes as per the elements, data types and their order. It is entirely generated by the JVM by a process known as hashing.

Hashing is internally a mathematically formula that converts data (may be large size) or object or collection class into a number. Infact, the hash code is used by Hashtable to search for the value when a key is given. Two objects get the same hash code number when they are equal. hashCode() method is used in combination with equals() method to compare two data structures or objects or arrays. Hashcode is the fastest way of comparing two entities like data structures or strings.

Note: The Object class also includes a hashCode() method that converts any object into a hashcode number.

Following is the method signature

  • static int hashCode(int array1[]): Used to convert the array into hash code value. Hash code is useful to compare two arrays. It is the fastest way of comparing two arrays. Overloaded to take any data type array as parameter. Comes into existence with JDK 1.5.
The following program on "Get hashcode with hashCode()" illustrates the usage of the above method.


Get hashcode with hashCode()
output screenshot on Get hashcode with hashCode()

String stones1[] = { “diamond”, “ruby”, “emerald” };
String stones2[] = { “diamond”, “ruby”, “emerald” };
String stones3[] = { “diamond”, “ruby”, “topaz” };

Three string array objects stones1, stones2 and stones3 are created. Observe, the elements of stones1 and stones2 are same and stones1 and stones3 are different.

System.out.println(“\nstones1 elements: ” + Arrays.toString(stones1));

Using toString() method of Arrays class, the elements of the three arrays are printed. toString() method makes it easier to print the elements of an array avoiding lengthy for loops.

int hc1 = Arrays.hashCode(stones1);
int hc2 = Arrays.hashCode(stones2);
int hc3 = Arrays.hashCode(stones3);

The hashCode() method of Arrays returns the hash code number of the array.

System.out.println(“\nstones1 and stones2 contain same elements: ” + (hc1 == hc2));
System.out.println(“stones1 and stones3 contain same elements: ” + (hc1 == hc3));

The first statement logical equal comparison, hc1 == hc2, returns true as the hash codes of stones1 and stones2 are same (they contain same elements) and hc1 == hc3, returns false as stones1 and stones3 contain different elements.

if(hc1 == hc2)

The hash code can be used with if control structure as in the above statement.

Leave a Reply

Your email address will not be published. Required fields are marked *