搜索
首页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结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java数据结构之AVL树详解Java数据结构之AVL树详解Jun 01, 2022 am 11:39 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

一文掌握Java8新特性Stream流的概念和使用一文掌握Java8新特性Stream流的概念和使用Jun 23, 2022 pm 12:03 PM

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。

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脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具