The hashCode() method of Java Integer class returns a hash code for this Integer.
Syntax
- public int hashCode()
- public static int hashCode(int value)
Parameters
The parameter ‘value’ represents a value whose hash code is to be determined.
Overrides
The hashCode() method overrides hashCode in class Object.
Return Value
This method returns a hash Code value for this Integer.
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 |
public class JavaIntegerHashCodeExample1 { public static void main(String[] args) { Integer val=45; //returns a hash code for this Integer int hash=val.hashCode(); System.out.println("Hash code for "+val+" is "+hash); Integer va11=78; System.out.println("Hash code for "+va11+" is "+va11.hashCode()); } } |
Output
1 2 3 4 |
Hash code for 45 is 45 Hash code for 78 is 78 |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 |
public class JavaIntegerHashCodeExample2 { public static void main(String[] args) { Integer val=45; //calling a static hash code method int hash=Integer.hashCode(val); System.out.println("Hash code for "+val+" is "+hash); Integer va11=78; System.out.println("Hash code for "+va11+" is "+Integer.hashCode(va11)); } } |
Output
1 2 3 4 |
Hash code for 45 is 45 Hash code for 78 is 78 |