Collections

Java hashCode() method in Java Code With Example

Table of Contents

Program

import java.util.Arrays;
public class HashCode 
{
	public static void main(String[] args) 
	{
		boolean [] b1 = new boolean [] {true};
        	boolean [] b2 = new boolean [] {false};
        	int O1= b1.hashCode();
        	System.out.println("b1 hash code :" +O1);
        	int O2= b2.hashCode();
        	System.out.println("b2 hash code :" +O2);
        	char [] c1 = new char [] {'a','b'};
        	int O3 = c1.hashCode();
        	System.out.println("char hash code :" +O3);
        	double [] d1 = new double [] {132,23};
        	int O4 = d1.hashCode();
        	System.out.println("double hash code :" +O4);
        	float [] f1 = new float [] {(float) 4.2,(float) 2.1};
         	int O5 = f1.hashCode();
        	System.out.println("float hash code :" +O5);
        	byte [] by = new byte [] {10,12};
        	int O6 = by.hashCode();
        	System.out.println("byte hash code :" +O6);
        }

}

Output

        b1 hash code :405662939
	b2 hash code :123961122
	char hash code :1227229563
	double hash code :1982791261
	float hash code :1562557367
	byte hash code :1101288798 */

Description

public int hashCode()

Returns a hash code for this Boolean object.

Overrides:

hashCode in class Object

Returns:

the integer 1231 if this object represents true; returns the integer 1237 if this object represents false.

See Also:

object.equals(java.lang.Object), System.identityHashCode(java.lang.Object)

public static int hashCode​(char value)

Returns a hash code for a char value; compatible with Character.hashCode().

Parameters:

value – The char for which to return a hash code.

Returns:

a hash code value for a char value.

Since:

1.8

public int hashCode()

Returns a hash code for this Double-object. The result is the exclusive OR of the two halves
of the long integer bit representation, exactly as produced by the method doubleToLongBits(double),
of the primitive double value represented by this Double-object.
That is, the hash code is the value of the expression:(int)(v^(v>>>32))
where v is defined by:
long v = Double.doubleToLongBits(this.doubleValue());

Overrides:

hashCode in class Object

Returns:

a hash code value for this object.

See Also:

Object.equals(java.lang.Object), System.identityHashCode(java.lang.Object)

public int hashCode()

Returns a hash code for this Float object. The result is the integer bit representation,
exactly as produced by the method floatToIntBits(float),
of the primitive float value represented by this Float object.

Overrides:

hashCode in class Object

Returns:

a hash code value for this object.

See Also:

Object.equals(java.lang.Object), System.identityHashCode(java.lang.Object)

public int hashCode()

Returns a hash code for this Byte; equal to the result of invoking intValue().

Overrides:

hashCode in class Object

Returns:

a hash code value for this object.

See Also:

Object.equals(java.lang.Object), System.identityHashCode(java.lang.Object)