搜索
首页Javajava教程Java 哈希码()

Java 哈希码()

Aug 30, 2024 pm 04:20 PM
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() );
}
}

输出:

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
平台独立性如何使企业级的Java应用程序受益?平台独立性如何使企业级的Java应用程序受益?May 03, 2025 am 12:23 AM

Java在企业级应用中被广泛使用是因为其平台独立性。1)平台独立性通过Java虚拟机(JVM)实现,使代码可在任何支持Java的平台上运行。2)它简化了跨平台部署和开发流程,提供了更大的灵活性和扩展性。3)然而,需注意性能差异和第三方库兼容性,并采用最佳实践如使用纯Java代码和跨平台测试。

考虑到平台独立性,Java在物联网(物联网)设备的开发中扮演什么角色?考虑到平台独立性,Java在物联网(物联网)设备的开发中扮演什么角色?May 03, 2025 am 12:22 AM

JavaplaysigantroleiniotduetoitsplatFormentence.1)itallowscodeTobewrittenOnCeandrunonVariousDevices.2)Java'secosystemprovidesuseusefidesusefidesulylibrariesforiot.3)

描述一个方案,您在Java中遇到了一个特定于平台的问题以及如何解决。描述一个方案,您在Java中遇到了一个特定于平台的问题以及如何解决。May 03, 2025 am 12:21 AM

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

Java平台独立对开发人员有什么好处?Java平台独立对开发人员有什么好处?May 03, 2025 am 12:15 AM

Java'splatFormIndenceistificantBecapeitAllowSitallowsDevelostWriTecoDeonCeandRunitonAnyPlatFormwithAjvm.this“ writeonce,runanywhere”(era)橱柜橱柜:1)交叉plat formcomplibility cross-platformcombiblesible,enablingDeploymentMentMentMentMentAcrAptAprospOspOspOssCrossDifferentoSswithOssuse; 2)

将Java用于需要在不同服务器上运行的Web应用程序的优点是什么?将Java用于需要在不同服务器上运行的Web应用程序的优点是什么?May 03, 2025 am 12:13 AM

Java适合开发跨服务器web应用。1)Java的“一次编写,到处运行”哲学使其代码可在任何支持JVM的平台上运行。2)Java拥有丰富的生态系统,包括Spring和Hibernate等工具,简化开发过程。3)Java在性能和安全性方面表现出色,提供高效的内存管理和强大的安全保障。

JVM如何促进Java的'写作一次,在任何地方运行”(WORA)功能?JVM如何促进Java的'写作一次,在任何地方运行”(WORA)功能?May 02, 2025 am 12:25 AM

JVM通过字节码解释、平台无关的API和动态类加载实现Java的WORA特性:1.字节码被解释为机器码,确保跨平台运行;2.标准API抽象操作系统差异;3.类在运行时动态加载,保证一致性。

Java的较新版本如何解决平台特定问题?Java的较新版本如何解决平台特定问题?May 02, 2025 am 12:18 AM

Java的最新版本通过JVM优化、标准库改进和第三方库支持有效解决平台特定问题。1)JVM优化,如Java11的ZGC提升了垃圾回收性能。2)标准库改进,如Java9的模块系统减少平台相关问题。3)第三方库提供平台优化版本,如OpenCV。

说明JVM执行的字节码验证的过程。说明JVM执行的字节码验证的过程。May 02, 2025 am 12:18 AM

JVM的字节码验证过程包括四个关键步骤:1)检查类文件格式是否符合规范,2)验证字节码指令的有效性和正确性,3)进行数据流分析确保类型安全,4)平衡验证的彻底性与性能。通过这些步骤,JVM确保只有安全、正确的字节码被执行,从而保护程序的完整性和安全性。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具