首页  >  文章  >  Java  >  Java 哈希码()

Java 哈希码()

PHPz
PHPz原创
2024-08-30 16:20:36691浏览

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn