>  기사  >  Java  >  자바 해시코드()

자바 해시코드()

PHPz
PHPz원래의
2024-08-30 16:20:36689검색

Java 프로그래밍 언어의 hashcode() 메소드는 항상 객체 클래스에 존재합니다. 따라서 모든 Java 프로그래밍 클래스에 대해 hashcode() 메서드의 기본 구현이 제공됩니다. 이 hashcode() 메서드는 객체의 정수 해시코드 값이며 네이티브 메서드입니다. hashcode() 메서드의 다중/다중 호출은 동일한 정수 값을 반환해야 하지만 이는 객체가 수정된 경우에만 수행되며 이는 equals() 메서드에서도 사용됩니다. hashcode() 개체 값은 동일한 응용 프로그램의 여러/다중 실행을 변경할 수 있습니다. 해시 코드는 Java의 각 개체와 관련된 정수 값일 뿐입니다.

구문:

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

public int hashcode()

Java에서 hashCode() 메소드는 어떻게 작동하나요?

hashcode() 메소드는 일부 해시코드 값을 정수로 반환하여 Java에서 작동합니다. 이 해시코드 정수 값은 HashMap, HashTable, HashSet 등과 같은 일부 해싱 기반 컬렉션에서 광범위하게 사용됩니다. Java의 hashcode() 메서드는 모든 클래스에서 재정의되어야 하며, 이는 equal()과 같은 메서드를 재정의하는 데 도움이 됩니다. .

애플리케이션을 실행하는 동안 특정 동일한 객체에 대해 hashcode() 메서드가 두 번 이상 호출되면 hashcode()는 동일한 정수 값을 일관되게 반환합니다. 정수 값은 특정 애플리케이션의 한 실행부터 동일한 앱/애플리케이션의 다른 실행까지 동일하게 유지됩니다. 2개의 객체가 동일하면 Java 코딩 언어의 hashcode() 메소드는 동일한 정수를 생성하고 2개의 객체 각각에 대해 이 두 객체가 동일하거나 동일하지 않으면 다음과 같은 정수 값을 생성합니다. 서로 다른 두 개체 중 하나에 대해 hashcode() 메서드에 의해 생성됩니다. 비슷하겠지만 두 개체 모두에 대해 서로 다른 정수 값을 생성하는 것이 HashTable, HashMap 등과 같은 해싱 성능 기반 컬렉션을 개선하는 데 더 좋습니다/최적입니다.

동일/유사 개체는 개체가 최종 범위까지 동일할 때 동일한 해시 코드를 생성합니다. hashcode()의 동일하지 않은 객체는 일부 고유하거나 다른 해시 코드를 생성하지 않습니다.

Java hashCode() 구현 예시

다음은 언급된 예시입니다.

예시 #1

블로그 링크와 일부 텍스트를 해시코드 변환으로 변환한 예입니다. 먼저 공개 클래스 “StringExample1”이 생성됩니다. 그 전에 이 프로그램에 대한 StringExample1.java라는 이름으로 프로그램 파일을 저장합니다. 그런 다음 Java 프로그램 코드를 입력하기 위한 main() 함수가 생성됩니다. 그런 다음 "profitloops.com" 값을 사용하여 "blogName1"이라는 문자열 변수가 생성됩니다. 그런 다음 다시 일부 문자열 텍스트를 사용하여 textprint1 변수가 생성됩니다.

그런 다음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

이것은 다양한 유형의 문자, 텍스트, Null 값 등에 대한 hashcode()의 예입니다. hashcode()가 어떻게 바뀌었는지 알고 hashcode()가 있을 때 특정 입력의 결과가 어떻게 될지 알 수 있습니다. 사용된. 먼저 Public 클래스 “StringExample1”을 생성하고, 프로그램 코드를 입력하기 위한 Public 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() );
}
}

출력:

자바 해시코드()

예시 #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.

위 내용은 자바 해시코드()의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:Java 명명 규칙다음 기사:Java 명명 규칙