HashCode (1) in Java: Basic Principles of the hash algorithm

Source: Internet
Author: User

I. Why does Hash algorithm exist?
Java has two types of collections: List and Set. The elements in the List are ordered and can be repeated. The Set element is unordered, but the element cannot be repeated. If we want to ensure that the elements are not repeated, what is the basis for determining whether the two elements are repeated? Use the Object. equals method. If an element is checked every time it is added, when there are many elements, the number of times that the elements added to the set are compared is very large. That is to say, if there are 1000 elements in the Set, when 1,001st elements are added to the set, it will call the 1000 equals method. This will obviously greatly reduce the efficiency. So Java adopts the principle of hash table. Hash is a personal name. As he proposed the concept of Hash algorithm, he named it by his name.
 
 
Ii. Principles of the Hash algorithm
When a Set receives an element, it calculates the hashCode Based on the memory address of the object to see which interval it belongs to. In this interval, the equeals method is called.

 
It indeed improves efficiency. But there is a problem: if the two objects equals are equal but not in the same range, there is no chance to compare them, they will be considered different objects. Therefore, Java specifies the eqauls method and hashCode method as follows:
1. If two objects are the same, their hashCode values must be the same. It also tells us to rewrite the equals method. We must rewrite the hashCode method.
2. If the hashCode of the two objects is the same, they are not necessarily the same. The same object here refers to comparison using the eqauls method.
 
 
Iii. Example
 
1. The hashCode and equals methods are not rewritten.
Package cn. xy. test;
Public class Point1
{
Private int x;
Private int y;
Public Point1 (int x, int y)
{
Super ();
This. x = x;
This. y = y;
}
Public int getX ()
{
Return x;
}
Public void setX (int x)
{
This. x = x;
}
Public int getY ()
{
Return y;
}
Public void setY (int y)
{
This. y = y;
}
}
Public class HashSetAndHashCode
{
Public static void main (String [] args)
{
HashSet <Point1> hs1 = new HashSet <Point1> ();
Point1 p11 = new Point1 (3, 3 );
Point1 p12 = new Point1 (3, 3 );
Point1 p13 = new Point1 (3, 5 );
Hs1.add (p11 );
Hs1.add (p11 );
Hs1.add (p12 );
Hs1.add (p13 );
System. out. println (hs1.size ());
}
}
The answer is 3.
 

2. Override hashCode and equals Methods
Package cn. xy. test;
Public class Point2
{
Private int x;
Private int y;
Public Point2 (int x, int y)
{
Super ();
This. x = x;
This. y = y;
}
@ Override
Public int hashCode ()
{
Final int prime = 31;
Int result = 1;
Result = prime * result + x;
Result = prime * result + y;
Return result;
}
@ Override
Public boolean equals (Object obj)
{
If (this = obj) return true;
If (obj = null) return false;
If (getClass ()! = Obj. getClass () return false;
Point2 other = (Point2) obj;
If (x! = Other. x) return false;
If (y! = Other. y) return false;
Return true;
}
Public int getX ()
{
Return x;
}
Public void setX (int x)
{
This. x = x;
}
Public int getY ()
{
Return y;
}
Public void setY (int y)
{
This. y = y;
}
}
Public class HashSetAndHashCode
{
Public static void main (String [] args)
{
HashSet <Point2> hs2 = new HashSet <Point2> ();
Point2 p21 = new Point2 (3, 3 );
Point2 p22 = new Point2 (3, 3 );
Point2 p23 = new Point2 (3, 5 );
Hs2.add (p21 );
Hs2.add (p22 );
Hs2.add (p23 );
System. out. println (hs2.size ());
}
}
The answer is 2. P21-p22 and p22 are considered to be the same object.
 

3. The hashCode method is not rewritten, but the equals method is rewritten.
Package cn. xy. test;
Public class Point3
{
Private int x;
Private int y;
Public Point3 (int x, int y)
{
Super ();
This. x = x;
This. y = y;
}
@ Override
Public boolean equals (Object obj)
{
If (this = obj) return true;
If (obj = null) return false;
If (getClass ()! = Obj. getClass () return false;
Point3 other = (Point3) obj;
If (x! = Other. x) return false;
If (y! = Other. y) return false;
Return true;
}
Public int getX ()
{
Return x;
}
Public void setX (int x)
{
This. x = x;
}
Public int getY ()
{
Return y;
}
Public void setY (int y)
{
This. y = y;
}
}
Public class HashSetAndHashCode
{
Public static void main (String [] args)
{
HashSet <Point3> hs3 = new HashSet <Point3> ();
Point3 p31 = new Point3 (3, 3 );
Point3 p32 = new Point3 (3, 3 );
Point3 p33 = new Point3 (3, 5 );
Hs3.add (p31 );
Hs3.add (p32 );
Hs3.add (p33 );
System. out. println (hs3.size ());
}
}
It may be 2 or 3. Because the hashcode calculated based on the memory address does not know whether it is in a region.

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.