当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Character hashCode()用法及代码示例


Character 类的 hashCode() 方法通常返回给定字符的哈希码。给定方法的结果类似于调用 charValue() 方法的结果。

用法

public int hashCode()

参数

不适用

返回值

Character 类的 hashCode() 方法返回给定字符的哈希码。

例子1

public class JavaCharacterhashCodeExample1 {
public static void main(String[] args) {
	// Create two Character objects char1 and char2.
	     Character char1, char2;
	// Assign the values to char1 and char2.
	     char1 = new Character('A');
	char2 = new Character('a');	
	// Create twoint primitives obj1, obj2
	int obj1, obj2;
// Assign the hashcodes of char1 and char2 to obj1 and obj2.
	obj1 = char1.hashCode();
	obj2 = char2.hashCode();
String str1 = "The hashcode for the first character '" + char1 + "' is given as:" + obj1;
String str2 = "The hashcode for the second character '" + char2 + "' is given as:" + obj2;
	// Print the values of obj1 and obj2.
	System.err.println(str1 );
	System.err.println(str2 );
	 }
}

输出:

The hashcode for the first character 'A' is given as:65
The hashcode for the second character 'a' is given as:97

例子2

public class JavaCharacterhashCodeExample2 {
	public static void main(String args[])
	  {
	    Character char1 = new Character('M');
	    Character char2 = new Character('N');
	    Character char3 = new Character('O');	
	    Character char4 = new Character('a');
	    Character char5 = new Character('b');
	    Character char6 = new Character('c');	
	int obj1 = char1.hashCode();
	int obj2 = char2.hashCode();
	int obj3 = char3.hashCode();
	int obj4 = char4.hashCode();
	int obj5 = char5.hashCode();
	int obj6 = char6.hashCode();
	System.err.println("The hash code for 'M' is given as:" + obj1); 
	System.err.println("The hash code for 'N is given as: " + obj2);
	System.err.println("The hash code for 'O' is given as:" + obj3);
	System.out.println("The hash code for 'a' is given as:" + obj4);
	System.out.println("The hash code for 'b' is given as:" + obj5);
	System.out.println("The hash code for 'c' is given as:" + obj6);
	}
}

输出:

The hash code for 'M' is given as:77
The hash code for 'N is given as: 78
The hash code for 'O' is given as:79
The hash code for 'a' is given as:97
The hash code for 'b' is given as:98
The hash code for 'c' is given as:99

例子3

public class JavaCharacterhashCodeExample3 {
	public static void main(String args[])
	  {
	    Character char1 = new Character('1');
	    Character char2 = new Character('2');
	    Character char3 = new Character('3');		
	int obj1 = char1.hashCode();
	int obj2 = char2.hashCode();
	int obj3 = char3.hashCode();
	System.err.println("The hash code for '1' is given as:" + obj1); 
	System.err.println("The hash code for '2' is given as: " + obj2);
	System.err.println("The hash code for '3' is given as:" + obj3);
	    }
}

输出:

The hash code for '1' is given as:49
The hash code for '2' is given as: 50
The hash code for '3' is given as:51

Java 字符 hashCode(char value) 方法

Character 类的 hashCode(char value) 方法通常为给定的 char 值返回一个 hash code 类型的值。给定方法的结果类似于 Character.hashCode() 方法的结果。

用法

public static int hashCode(char value)

参数

value: 它是需要返回哈希码的字符值。

返回值

hashCode(char value) 方法为给定的 char 值返回一个哈希码类型的值。

示例 4

public class JavaCharacterhashCode_Example1{
	public static void main(String[] args) {
		// Create two char primitives obj1 and obj2.
	char obj1, obj2;
	// Assign the values to obj1 and obj2.
	obj1 = 'D';
	obj2= 'd';
// Create two int primitives b1 and b2.
	int b1, b2;
// Assign the hash code of obj1 and obj2 to b1 and b2
	b1 = Character.hashCode(obj1);
	b2 = Character.hashCode(obj2);	
	System.err.println("The hash code for the first character is given as:" + b1);
	System.err.println("The hash code for the second character is given as:" + b2); 
	   }
	}

输出:

The hash code for the first character is given as:68
The hash code for the second character is given as:100

例 5

public class JavaCharacterhashCode_Example2 {
	public static void main(String[] args) {
		// Create three char primitives obj1, obj2 and obj3.
	char obj1, obj2,obj3;
	// Assign the values to obj1, obj2 and obj3.
	obj1 = '1';
	obj2 = '3';
	obj3 = '5';
// Create three int primitives b1, b2 and b3.
	int b1, b2,b3;
// Assign the hash codes.
	b1 = Character.hashCode(obj1);
	b2 = Character.hashCode(obj2);	
	b3 = Character.hashCode(obj3);	
	System.err.println("The hash code for the first character is given as:" + b1);
	System.err.println("The hash code for the second character is given as:" + b2); 
	System.err.println("The hash code for the third character is given as:" + b3); 
	   }
	}

输出:

The hash code for the first character is given as:49
The hash code for the second character is given as:51
The hash code for the second character is given as:53



相关用法


注:本文由纯净天空筛选整理自 Java Character hashCode() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。