首頁 > Java > java教程 > 主體

Java 哈希碼()

PHPz
發布: 2024-08-30 16:20:36
原創
722 人瀏覽過

Java 程式語言的 hashcode() 方法總是存在於物件類別中。因此,每個 Java 程式類別都會獲得 hashcode() 方法的預設實作。這個hashcode()方法是物件的整數hashcode值,它是一個native方法。多次呼叫 hashcode() 方法必須傳回相同的整數值,但只有在物件被修改時才會執行此操作,這也用在 equals() 方法中。 hashcode() 物件值可以更改相同應用程式的多次/多次執行。哈希碼只不過是與 Java 中每個物件關聯的整數值。

文法:

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

public int hashcode()
登入後複製

Java 中的 hashCode() 方法是如何運作的?

hashcode() 方法在 java 中運作,透過傳回一些 hashcode 值作為整數。這個hashcode整數值在一些基於雜湊的集合中大量使用,例如HashMap、HashTable、HashSet等。在每個類別中都需要重寫Java的hashcode()方法,這有助於重寫equal()等方法.

在執行應用程式期間,如果對相同物件多次呼叫 hashcode() 方法,則 hashcode() 將一致傳回相同的整數值。從特定應用程式的僅一次執行到相同應用程式/應用程式的其他一些執行,該整數值將保持不變。如果這兩個物件相等,那麼Java 編碼語言的hashcode() 方法將在這兩個物件中的每一個上產生相同的整數,如果這兩個物件不相等/不相等,則產生的整數值將是由hashcode() 方法在兩個不同的物件之一上產生。它會類似,但在所有兩個物件中的每一個上產生不同的整數值對於提高基於哈希效能的集合(如 HashTable、HashMap .. 等)來說更好/最好。

當物件在最終範圍內相等時,相等/相似的物件將產生相同的雜湊碼。 hashcode() 的不相等物件不會產生一些不同/不同的雜湊碼。

實作 Java hashCode() 的範例

以下是提到的範例:

範例#1

這是一個將部落格連結和一些文字轉換為雜湊碼轉換的範例。首先,建立一個公共類別“StringExample1”。在此之前,將程式儲存為名稱為StringExample1.java的程式檔案。然後建立main()函數,輸入java程式碼。然後建立一個名為「blogName1」的字串變量,其值為「profitloops.com」。然後,再次使用一些字串文字建立一個 textprint1 變數。

然後使用hashcode()函數將profitloops.com轉換為hashcode。同樣,對於其他字串文本,該字串也將轉換為哈希碼。若要列印這些字串文字的雜湊碼,請使用“System.out.println()”函數。然後關閉括號以達到正確的語法目的。查看下面的輸出,以便您了解哈希碼是如何轉換的。

代碼:

public class StringExample1{
public static void main(String[] args)
{
String blogName1 = "profitloops.com";
String textprint1 = "This is the Hashcode of the 'profitloops.com :: '";
System.out.println(textprint1);
System.out.println( blogName1.hashCode() );
System.out.println("This is the Hashcode of the string 'EDUCBA :: '");
System.out.println( "EDUCBA".hashCode() );
}
}
登入後複製

輸出:

Java 哈希碼()

範例#2

這是hashcode()在各種類型的字元、文字、空值等上的範例。了解hashcode()如何轉變以及知道hashcode()時特定輸入的結果是什麼用過的。首先建立一個公共類別“StringExample1”,並建立公共main來輸入程式碼。然後使用 hashcode() 來了解 NULL 元素、Space 元素的雜湊碼是什麼。這裡,名為「EDUCBA」、「physical」和「PHYSICS」的字串、小字母「a」、大字母「b」在 hashcode() 函數的幫助下轉換為雜湊碼。 System.out.println() 函數用於顯示終端機或命令提示字元的輸出或任何其他顯示不同類型字串值或任何其他值的雜湊碼值。

代碼:

public class StringExample1{
public static void main(String[] args)
{
String blogName1 = "";
System.out.println("This is the Hashcode of the Null Element::");
System.out.println( "".hashCode() );
System.out.println("This is the HashCode of Space Element::");
System.out.println( " ".hashCode() );
System.out.println("This is the Hashcode of the string 'EDUCBA :: '");
System.out.println( "EDUCBA".hashCode() );
System.out.println("This is the HashCode of the alphabet small a::");
System.out.println( "a".hashCode() );
System.out.println("This is the HashCode of the alphabet big A::");
System.out.println( "A".hashCode() );
System.out.println("This is the HashCode of the word physics::");
System.out.println( "physics".hashCode() );
System.out.println("This is the HashCode of the word PHYSICS::");
System.out.println( "PHYSICS".hashCode() );
}
}
登入後複製

輸出:

Java 哈希碼()

範例#3

這是一個為不同變數值實作雜湊碼的範例。如果兩個變數相等,它們將提供相等變數的輸出,然後提供這些變數的雜湊碼。同樣,如果兩個變數值不相等,那麼在不相等的變數下,這些變數的雜湊碼也會被列印。

At first, public class “hash1” is created, and the public main is created. Then two variables, a1 and b1, are created with the same values. Then those two variable values are checked. If same, a1 and b1 hashcode values are printed. Here a1 and b1 are the same. Then string values c1 and d1 are created with different string values like 10 and 50. String values c1 and d1 are checked to know whether they are equal or not. Here not equal so “Unequal variables:” text is printed, and then hash codes of those variable values are printed. Check out the output of this hashcode() example so that you will understand the hashcode() concept better.

Code:

public class hash1{
public static void main(String[] args){
String a1 = "200";
String b1 = "200";
if(a1.equals(b1)){
System.out.println("Equal variables: \n");
System.out.println("A1 variable 200's hashcode :: "+a1.hashCode() + "\n B1 variable value 200's hashcode :: " + b1.hashCode()+"\n");
}
String c1 = "10";
String d1 = "50";
if(!c1.equals(d1)){
System.out.println("\nUn-equal variables: \n");
System.out.println("C1 variable value 10's hash code :: "+c1.hashCode() + "\nD1 variable value 50's hashcode :: " + d1.hashCode()+"\n");
}
}
}
登入後複製

Output:

Java 哈希碼()

Conclusion

We hope you learned what the definition of the Java hashcode() method and its syntax and its explanation is, How the hashcode() method of the Java Programming Language works with various examples to understand the concept better and so easily.

以上是Java 哈希碼()的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!