當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。