Differences and connections between hashcode and equal Methods

Source: Internet
Author: User

Hashcode and equal method Overloading

1. Why do I need to overload the equal method?

Answer: by default, the equal method of an object compares two objects, which means that the address is equal to the same memory; otherwise, the address is not equal; if you need to use the values in the object to determine whether the values are equal, the equal method is overloaded.

2. Why is the hashcode method overloaded?

Answer: In general, hashcode does not need to be reloaded. hashcode is reloaded only when the class needs to be placed in a set of hash structures such as hashtable, hashmap, and hashset. Why does hashcode need to be reloaded? For hashmap, hashmap is a large memory block with many small memory blocks and a series of objects, you can use hashcode to find the small memory block hashcode % size (the number of small memory blocks), so when equal is equal, hashcode must be equal, and if it is an object, the hashcode and equal methods must be reloaded.

3. Why must hashcode be equal when equals () is equal, while hashcode is equal, but equals is not required?

Answer: 1. Because hashcode is used to access small memory blocks, hashcode must be equal.

2. Obtain an object from hashmap. The hashcode of the key is equal and the equal value is true.

The reason why hashcode is equal, but equal can be used. For example, if both objecta and objectb have the attribute name, hashcode is calculated by name, so hashcode is the same, but the two objects belong to different types, therefore, equal is false.

4. Why hashcode?

1. You can quickly find small memory blocks through hashcode.
2. The hashcode method is faster than the equal method. When get is used, hashcode is compared first. If hashcode is different, false is returned directly.

 

The following is the instance code of a specific class:

 1 public class Person 2 { 3  private String name; 4  private int age; 5  6  @Override 7  public int hashCode() 8  { 9   final int prime = 31;10   int result = 1;11   result = prime * result + age;12   result = prime * result + ((name == null) ? 0 : name.hashCode());13   return result;14  }15 16  @Override17  public boolean equals(Object obj)18  {19   if (this == obj)20    return true;21   if (obj == null)22    return false;23   if (getClass() != obj.getClass())24    return false;25   Person other = (Person) obj;26   if (age != other.age)27    return false;28   if (name == null)29   {30    if (other.name != null)31     return false;32   } else if (!name.equals(other.name))33    return false;34   return true;35  }36 }

 

[Transfer] http://hi.baidu.com/bluedream348/item/6f4fa3a468816ed85af1914e

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.