Java judges whether two objects are equal to equals and hashcode ().

Source: Internet
Author: User

Note hashcode only when hashtable, hashmap, hashset, and linkedhashmap are used. hashcode is useless in other places. (This is not necessarily true)

Whether two objects are equal requires that hashcode () be equal. Is the following statement true?

In a Java set, the rule for determining whether two objects are equal is:
1), judge whether the hashcode of the two objects is equal
If they are not equal, the two objects are considered not equal.
If equal, transfer 2)
(This is only required to improve storage efficiency. In theory, it is not acceptable. However, if it is not, the actual usage of the aging rate will be greatly reduced. Therefore, we need it here. This issue will be highlighted later .)
2) determine whether two objects are equal using the equals operation
If they are not equal, the two objects are considered not equal.
If the two objects are equal, equals () is the key to determining whether the two objects are equal)

 

I personally think the above statement is: Hashset rules for determining whether elements are repeated(Because hashset does not allow repeated elements)

Instead of hashtable, hashmap, hashset, and linkedhashmap, you can call equals in the code to determine if they are equal.

 

Let's take a look at the example of the bottom edge of the trim.

Class Name
{
Private string first;
Private string last;

Public name (string first, string last)
{
This. First = first;
This. Last = last;
}

Public Boolean equals (Object O)
{
If (this = O)
{
Return true;
}

If (O. getclass () = Name. Class)
{
Name n = (name) O;
Return N. First. Equals (first)
& N. Last. Equals (last );
}
Return false;
}
}

Public class hashsettest
{
Public static void main (string [] ARGs)
{
Set <Name> S = new hashset <Name> ();
S. Add (new name ("ABC", "123 "));
System. Out. println (
S. Contains (new name ("ABC", "123 ")));
}
}

After adding a new name ("ABC", "123") object to the hashset, the program immediately checks whether the hashset contains a new name ("ABC ", "123") object. Rough look, it is easy to think that the program will output true.

In actual running, the above program will see that the program outputs false, because the hashset criteria for determining the equality between two objects require that the hashcode () of the two objects be required, in addition to comparing and returning true through the equals () method ()
Return values are equal. The above program did not override the hashcode () method of the name class. The values of hashcode () returned by two name objects are different. Therefore, hashset treats them as two objects, so the program returns false.

It can be seen that when we try to treat the object of a class as the key of hashmap, or try to save the object of this class into hashset, We will overwrite the equals (Object OBJ) of this class) the methods and hashcode () methods are very important, and the return values of these two methods must be consistent: when the two hashcode () return values of the class are the same, they use equals () if the method is compared, return true. Generally, all key attributes involved in calculating the return value of hashcode () should be used as equals ()
Comparison criteria.
The following program correctly overrides the hashcode () and equals () Methods of the name class. The program is as follows:

Java code
Class Name
{
Private string first;
Private string last;
Public name (string first, string last)
{
This. First = first;
This. Last = last;
}
// Judge whether two names are equal based on first
Public Boolean equals (Object O)
{
If (this = O)
{
Return true;
}
If (O. getclass () = Name. Class)
{
Name n = (name) O;
Return N. First. Equals (first );
}
Return false;
}

// Calculate the hashcode () Return Value of the name object based on first
Public int hashcode ()
{
Return first. hashcode ();
}

Public String tostring ()
{
Return "name [first =" + first + ", last =" + last + "]";
}
}

Public class hashsettest2
{
Public static void main (string [] ARGs)
{
Hashset <Name> set = new hashset <Name> ();
Set. Add (new name ("ABC", "123 "));
Set. Add (new name ("ABC", "456 "));
System. Out. println (SET );
}
}

The above program provides a name class, which overrides the equals () and tostring () methods. Both methods are determined based on the first instance variable of the name class, when the first instance variables of the two name objects are the same, the hashcode () return values of the two name objects are the same, and true is returned for comparison through equals.

The main program method first adds the first name object to the hashset. The first instance variable value of this name object is "ABC ", next, the program tries to add a name object named first "ABC" to the hashset. Obviously, the new name object cannot be added to the hashset, because the first of the name object to be added here is "ABC", hashset judges that the new name object is the same as the original name object, so it cannot be added, when the Set set is output in code ①, the set contains only one
The name object is the first name object whose last is "123.

 

At the underlying layer of a hashset, hashmap is used to store all elements. In the key that is stored with a hashmap, the key of the defined hashmap determines whether the key is equal and whether the hashcode and equals are equal.

 

From http://blog.csdn.net/willielee/article/details/5804463

Note hashcode only when hashtable, hashmap, hashset, and linkedhashmap are used. hashcode is useless in other places. (This is not necessarily true)

Whether two objects are equal requires that hashcode () be equal. Is the following statement true?

In a Java set, the rule for determining whether two objects are equal is:
1), judge whether the hashcode of the two objects is equal
If they are not equal, the two objects are considered not equal.
If equal, transfer 2)
(This is only required to improve storage efficiency. In theory, it is not acceptable. However, if it is not, the actual usage of the aging rate will be greatly reduced. Therefore, we need it here. This issue will be highlighted later .)
2) determine whether two objects are equal using the equals operation
If they are not equal, the two objects are considered not equal.
If the two objects are equal, equals () is the key to determining whether the two objects are equal)

 

I personally think the above statement is: Hashset rules for determining whether elements are repeated(Because hashset does not allow repeated elements)

Instead of hashtable, hashmap, hashset, and linkedhashmap, you can call equals in the code to determine if they are equal.

 

Let's take a look at the example of the bottom edge of the trim.

Class Name
{
Private string first;
Private string last;

Public name (string first, string last)
{
This. First = first;
This. Last = last;
}

Public Boolean equals (Object O)
{
If (this = O)
{
Return true;
}

If (O. getclass () = Name. Class)
{
Name n = (name) O;
Return N. First. Equals (first)
& N. Last. Equals (last );
}
Return false;
}
}

Public class hashsettest
{
Public static void main (string [] ARGs)
{
Set <Name> S = new hashset <Name> ();
S. Add (new name ("ABC", "123 "));
System. Out. println (
S. Contains (new name ("ABC", "123 ")));
}
}

After adding a new name ("ABC", "123") object to the hashset, the program immediately checks whether the hashset contains a new name ("ABC ", "123") object. Rough look, it is easy to think that the program will output true.

In actual running, the above program will see that the program outputs false, because the hashset criteria for determining the equality between two objects require that the hashcode () of the two objects be required, in addition to comparing and returning true through the equals () method ()
Return values are equal. The above program did not override the hashcode () method of the name class. The values of hashcode () returned by two name objects are different. Therefore, hashset treats them as two objects, so the program returns false.

It can be seen that when we try to treat the object of a class as the key of hashmap, or try to save the object of this class into hashset, We will overwrite the equals (Object OBJ) of this class) the methods and hashcode () methods are very important, and the return values of these two methods must be consistent: when the two hashcode () return values of the class are the same, they use equals () if the method is compared, return true. Generally, all key attributes involved in calculating the return value of hashcode () should be used as equals ()
Comparison criteria.
The following program correctly overrides the hashcode () and equals () Methods of the name class. The program is as follows:

Java code
Class Name
{
Private string first;
Private string last;
Public name (string first, string last)
{
This. First = first;
This. Last = last;
}
// Judge whether two names are equal based on first
Public Boolean equals (Object O)
{
If (this = O)
{
Return true;
}
If (O. getclass () = Name. Class)
{
Name n = (name) O;
Return N. First. Equals (first );
}
Return false;
}

// Calculate the hashcode () Return Value of the name object based on first
Public int hashcode ()
{
Return first. hashcode ();
}

Public String tostring ()
{
Return "name [first =" + first + ", last =" + last + "]";
}
}

Public class hashsettest2
{
Public static void main (string [] ARGs)
{
Hashset <Name> set = new hashset <Name> ();
Set. Add (new name ("ABC", "123 "));
Set. Add (new name ("ABC", "456 "));
System. Out. println (SET );
}
}

The above program provides a name class, which overrides the equals () and tostring () methods. Both methods are determined based on the first instance variable of the name class, when the first instance variables of the two name objects are the same, the hashcode () return values of the two name objects are the same, and true is returned for comparison through equals.

The main program method first adds the first name object to the hashset. The first instance variable value of this name object is "ABC ", next, the program tries to add a name object named first "ABC" to the hashset. Obviously, the new name object cannot be added to the hashset, because the first of the name object to be added here is "ABC", hashset judges that the new name object is the same as the original name object, so it cannot be added, when the Set set is output in code ①, the set contains only one
The name object is the first name object whose last is "123.

 

At the underlying layer of a hashset, hashmap is used to store all elements. In the key that is stored with a hashmap, the key of the defined hashmap determines whether the key is equal and whether the hashcode and equals are equal.

 

From http://blog.csdn.net/willielee/article/details/5804463

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.