Java Character hashCode()及示例
Java.lang.Character.hashCode()是Java中的一个内置方法,用于返回该字符的哈希代码。返回的哈希代码等于调用charValue()的结果。
语法
public int hashCode()
返回值: 该方法返回该字符的哈希代码值。
以下程序说明了Java.lang.Character.hashCode()函数。
程序1 :
// Java program to demonstrate the
// function when the value passed in the parameter
// is a character
import java.lang.*;
public class Gfg {
public static void main(String[] args)
{
// parameter ch
char ch = 'B';
// assigns character values
Character c = Character.valueOf(ch);
// assign hashcodes of c1, c2 to i1, i2
int i = c.hashCode();
// prints the character values
System.out.println("Hashcode of " + ch + " is " + i);
}
}
输出:
Hashcode of B is 66
程序2 :
// Java program to demonstrate the
// function when the value passed in the parameter
// is a number
import java.lang.*;
public class Gfg {
public static void main(String[] args)
{
// parameter ch
char ch = '6';
// assigns character values
Character c = Character.valueOf(ch);
// assign hashcodes of ch
int i = c.hashCode();
// prints the character values
System.out.println("Hashcode of " + ch + " is " + i);
}
}
输出:
Hashcode of 6 is 54