Equals () and hashCode () methods in Java

Source: Internet
Author: User
Tags md5 hash

Hash function, hash algorithm, and hash function.
Is a method to create a small number "fingerprint" from any type of data.
The hash function compresses messages or data into summaries to reduce the data volume and fix the data format.
Good hash functions rarely conflict with hash columns in the input domain.
========================================================== ========================================================== =
All hash functions have the following basic features:
1: If a = B, h (a) = h (B ).
2: If! = B, h (a) and h (B) may obtain the same hash value.
========================================================== ========================================================== =
Common hash algorithms are described as follows:
(1) MD4
MD4 (RFC 1320) was designed by MIT's Ronald L. Rivest in 1990. MD is short for Message Digest.
It is applicable to high-speed software implementation on 32-bit character-length processors-it is implemented based on 32-bit operations.
(2) MD5
MD5 (RFC 1321) is an improved version of MD4 by Rivest in 1991. It still groups the input in 512 bits,
The output is a cascade of 4 32-bit characters, which is the same as that of md4. MD5 is more complex than MD4, and the speed is a little slower,
But it is safer and better in terms of anti-analysis and anti-difference performance.
(3) SHA-1 and others
SHA1 is designed to be used together with DSA by nist nsa. It generates a hash value with a length of less than 264 for an input with a length of bits,
Therefore, brute-force is better. SHA-1 is designed based on the same principle as MD4 and imitates this algorithm.
========================================================== ========================================================== =
Daily use of hash algorithms:
(1) file Verification
We are familiar with the parity and CRC verification algorithms. These two verification algorithms do not defend against data tampering,
To a certain extent, they can detect and correct channel codes in data transmission, but cannot prevent malicious data destruction.
The "digital fingerprint" feature of the MD5 Hash algorithm makes it the most widely used file integrity Checksum algorithm,
Many Unix systems provide md5 checksum calculation commands.
(2) Digital Signature
Hash algorithms are also an important part of modern cryptographic systems. Because asymmetric algorithms are slow in operation,
Therefore, in the Digital Signature Protocol, one-way hashing plays an important role. For the Hash value,
It is also called "digital digest" for digital signature. in statistics, it can be considered as equivalent to digital signature for the file itself.
This Protocol also has other advantages.
(3) Authentication Protocol
The following authentication protocols are also called challenges-Authentication Mode: the transmission channel can be listened on,
But it cannot be tampered with. This is a simple and secure method.
========================================================== ========================================================== =
Hash in Java

1. The conventions of java. lang. Object are as follows:
(1) If the hashCode () method is called multiple times on the same object, the same integer value is always returned.
(2) If a. equals (B), a. hashCode () must be equal to B. hashCode ().
(3) If! A. equals (B), then a. hashCode () is not necessarily equal to B. hashCode ().
If a. hashCode () is not equal to B. hashCode () at this time, the performance of hashtables will be improved.

========================================
Default implementation in Object
/**
* As much as is reasonably practical,
* The hashCode method defined by class Object does return distinct integers for distinct objects.
* (This is typically implemented by converting the internal address of the object into an integer,
* But this implementation technique is not required by the Java TM programming language .)
*/
Public native int hashCode ();


Public boolean equals (Object obj ){
Return (this = obj );
}
========================================
Default implementation in String
// The equals () method has nothing to do with hashCode ().
Public boolean equals (Object anObject ){
If (this = anObject ){
Return true;
}
If (anObject instanceof String ){
String anotherString = (String) anObject;
Int n = count;
If (n = anotherString. count ){
Char v1 [] = value;
Char v2 [] = anotherString. value;
Int I = offset;
Int j = anotherString. offset;
While (n --! = 0 ){
If (v1 [I ++]! = V2 [j ++])
Return false;
}
Return true;
}
}
Return false;
}


// String overwrites the hashCode () method.
Public int hashCode (){
Int h = hash;
Int len = count;
If (h = 0 & len> 0 ){
Int off = offset;
Char val [] = value;


For (int I = 0; I <len; I ++ ){
H = 31 * h + val [off ++];
}
Hash = h;
}
Return h;
}
========================================
Default implementation in Integer
// The equals () of Integer is hashCode () by default, while hashCod () is Integer. intValue () by default ()
Public boolean equals (Object obj ){
If (obj instanceof Integer ){
Return value = (Integer) obj). intValue ();
}
Return false;
}


// The hashCode of Integer is Integer. intValue () by default ()
Public int hashCode (){
Return value;
}


Public Integer (int value ){
This. value = value;
}
========================================
Implementation of HashMap. contains () is as follows:
/**
* Applies a supplemental hash function to a given hashCode,
* Which defends against poor quality hash functions.
*/
// This is an enhanced hash
Static int hash (int h ){
H ^ = (h >>> 20) ^ (h >>>> 12 );
Return h ^ (h >>> 7) ^ (h >>> 4 );
}


Public boolean containsKey (Object key ){
Return getEntry (key )! = Null;
}


Final Entry GetEntry (Object key ){
// Call key. hashCod () first, and then perform an enhanced hash.
Int hash = (key = null )? 0: hash (key. hashCode ());
For (Entry E = table [indexFor (hash, table. length)];
E! = Null;
E = e. next ){
Object k;
// Compare the hash value first, which must be equal, and then call the equals () method of the two objects.
If (e. hash = hash &&
(K = e. key) = key | (key! = Null & key. equals (k ))))
Return e;
}
Return null;
}
========================================================== ========================================================== =
Conclusion:
(1) You can understand the hash (or hash) in this way, compress the original text, generate an introduction (that is, the abstract), and have the following rules:
After compression of different texts, most of them can get different summaries, but a few produce the same abstract, that is, collision.
The same text must be compressed to get the same abstract.

(2): equals () We understand that the comparison text is the same, and hashCode () understands that the comparison abstract is the same.
(3): So
If equals () is the same, hashCode () must be the same.
Equals () is different, and most hashCode () is different, but a few generate the same hashCode (), that is, a collision.
(4): We can understand the HashMap. get (key) method in this way.
HashCode () is the abstract of an object, except that the digest is restricted by the closed interval [0, table. length-1] By the digest generation algorithm.
Generate a summary based on the key (I .e. the subscript in the array) and perform cyclic comparison on the linked list on this subscript
Under the premise that the abstract is the same (in fact, the abstract of all elements in the linked list is the same, which is the collision processing method ),
And the equals () method is the same (that is, the content that actually cares about), the correct value is returned.
(5): So,
If equals () is the same, but hashCode () is different,
If put (key, value) is used, the existing key will be incorrectly calculated and does not exist,
Therefore, an object with the same business logic is stored in other locations. The only difference between two objects is that the hash value is different.
However, get (key) is correct, that is, the value of the object with the same business logic is retrieved.


The test code is as follows:

package com.collonn.algorithm.test;import java.util.HashMap;import java.util.Random;class Stu {public String name;public int age;public int hash;public Stu() {}public Stu(String name, int age) {this.name = name;this.age = age;this.hash = new Random().nextInt(9);}@Overridepublic boolean equals(Object obj) {if (obj instanceof Stu) {Stu stu = (Stu) obj;if (stu.name.equals(this.name)) {return true;} else {return false;}}return false;}public int hashCode() {return this.hash;}}public class Test2 {private static void Print(Stu s) {System.out.printf("\n name=%s, age=%s, hash=%s", s.name, s.age, s.hash);}public static void main(String[] args) {Stu a = new Stu("a", 1);Stu b = new Stu("a", 2);Stu c = new Stu("a", 3);System.out.printf("\n ------ new ------");Print(a);Print(b);Print(c);HashMap
 
   map = new HashMap
  
   ();map.put(a, a);map.put(b, b);map.put(c, c);System.out.printf("\n ------ get ------");Stu sut = map.get(a);Print(sut);sut = map.get(b);Print(sut);sut = map.get(c);Print(sut);}}
  
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.