Java 哈希碼()
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() ); } }
輸出:
範例#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() ); } }
輸出:
範例#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:
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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

膠囊是一種三維幾何圖形,由一個圓柱體和兩端各一個半球體組成。膠囊的體積可以通過將圓柱體的體積和兩端半球體的體積相加來計算。本教程將討論如何使用不同的方法在Java中計算給定膠囊的體積。 膠囊體積公式 膠囊體積的公式如下: 膠囊體積 = 圓柱體體積 兩個半球體體積 其中, r: 半球體的半徑。 h: 圓柱體的高度(不包括半球體)。 例子 1 輸入 半徑 = 5 單位 高度 = 10 單位 輸出 體積 = 1570.8 立方單位 解釋 使用公式計算體積: 體積 = π × r2 × h (4

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。
