Java ハッシュコード()

PHPz
リリース: 2024-08-30 16:20:36
オリジナル
722 人が閲覧しました

Java プログラミング言語の hashcode() メソッドは常にオブジェクト クラスに存在します。したがって、すべての Java プログラミング クラスに対して、hashcode() メソッドのデフォルト実装が取得されます。この hashcode() メソッドはオブジェクトの整数ハッシュコード値であり、ネイティブ メソッドです。 hashcode() メソッドの複数回の呼び出しは同じ整数値を返す必要がありますが、これはオブジェクトが変更された場合にのみ実行され、equals() メソッドでも使用されます。 hashcode() オブジェクトの値は、同じアプリケーションの複数の実行を変更することができます。ハッシュ コードは、Java の各オブジェクトに関連付けられた整数値にすぎません。

構文:

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

public int hashcode()
ログイン後にコピー

Java では hashCode() メソッドはどのように機能しますか?

hashcode() メソッドは Java で機能し、ハッシュコード値を整数として返します。このハッシュコード整数値は、HashMap、HashTable、HashSet などの一部のハッシュ ベースのコレクションで広く使用されています。Java の hashcode() メソッドはすべてのクラスでオーバーライドする必要があり、これは、equal() などのメソッドをオーバーライドするのに役立ちます。 .

アプリケーションの実行中に、特定の同じオブジェクトに対して hashcode() メソッドが複数回呼び出された場合、hashcode() は一貫して同じ整数値を返します。整数値は、特定のアプリケーションの 1 回の実行から、同じアプリケーションの別の実行まで同じままです。 2 つのオブジェクトが等しい場合、Java コーディング言語の hashcode() メソッドは同じ整数を生成し、2 つのオブジェクトが等しいか等しくない場合は、それぞれのオブジェクトに対して次の整数値を生成します。 2 つのオブジェクトのうちの 1 つの別個のオブジェクトの hashcode() メソッドによって生成されます。似たものになりますが、2 つのオブジェクトすべてに異なる整数値を生成する方が、HashTable、HashMap などのハッシュ パフォーマンスベースのコレクションを改善するには最適です。

等しい/類似したオブジェクトは、オブジェクトが最終範囲まで等しい場合、同じハッシュ コードを生成します。 hashcode() の不等オブジェクトは、いくつかの個別の/異なるハッシュ コードを生成しません。

Java hashCode() の実装例

以下に挙げる例を示します:

例 #1

ブログリンクと一部のテキストをハッシュコード変換する例です。まず、パブリッククラス「StringExample1」を作成します。その前に、このプログラムのプログラム ファイルを StringExample1.java という名前で保存します。次に、Java プログラム コードを入力する main() 関数が作成されます。次に、「blogName1」という文字列変数が値「profitloops.com」で作成されます。次に、再度、文字列テキストを使用して textprint1 変数が作成されます。

次に、hashcode() 関数を作成して、profitloops.com をハッシュコードに変換します。他の文字列テキストも同様にハッシュコードに変換されます。これらの文字列テキストのハッシュ コードを出力するには、「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

これは、さまざまなタイプの文字、テキスト、Null 値などに対する hashcode() の例です。hashcode() がどのように変化したかを知り、hashcode() が次の場合に特定の入力の結果がどうなるかを知るためです。使用済み。まず、パブリッククラス「StringExample1」を作成し、プログラムコードを入力するためのパブリックmainも作成します。次に、 hashcode() を使用して、NULL 要素、Space 要素のハッシュ コードが何であるかを確認します。ここでは、「EDUCBA」、「physics」、「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

これは、さまざまな変数値のハッシュ コードを実装する例です。 2 つの変数が等しい場合、それらは等しい変数として出力を提供し、それらのハッシュ コードを提供します。同様に、2 つの変数値が等しくない場合、等しくない変数の下では、それらの変数のハッシュ コードも出力されます。

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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!