The role of Java Hashcode and the refactoring rules of the Equals method

Source: Internet
Author: User
Tags object object

This is the blogger's preliminary understanding of the hashcode, later deepened will be updated again:

1. What is Hashcode?

Hashcode is the object's hash code, different objects are almost the same, said almost because it can still be the same.

Features: Each object has a hashcode, and the default value is the address of each object.

2, the role of Hashcode:

Bloggers are known to facilitate the user to use a hash table to insert data, we know that there are two types of collections, list----can be repeated, set----not repeatable.

Where the set implementation is non-repeatable, you need to use the hashcode and equals methods.

The hash table set is implemented using a list of linked lists, each of which is called a bucket, and the location of the object in the lookup table is using the following method:

1), the calculated object hash code hashcode;

2), formula: hashcode% number of barrels = index; eg: the hash code for an object is 76268 and there are 128 buckets, so the corresponding object's position is in the 108th bucket.

The same process is used when inserting an object into a hash table, but this time it is possible to find that the original position is occupied and the bucket is full, which requires the Equals method to be judged equal.

So:

You must refactor the Hashcode method when you refactor the Equals method, or the bucket will not be found when using the hash table, let alone whether the next decision is the same object.

Here is the sample code:

    public static void Main (string[] args) {String hello = "Hello";        StringBuilder HELLOSB = new StringBuilder (hello);        String Hello2 = "Hello";        StringBuilder HELLO2SB = new StringBuilder (HELLO2);        System.out.println ("Hello ' s hashcode:" + hello.hashcode ());        System.out.println ("Hellosb ' s hashcode:" + hellosb.hashcode ());        System.out.println ("Hello2 ' s hashcode:" + hello2.hashcode ());        System.out.println ("Hello2sb ' s hashcode:" + hello2sb.hashcode ());        Set stringset = new HashSet ();        Set sbset = new HashSet ();        Stringset.add (hello);        System.out.println ("======" + stringset.contains (Hello2));        Stringset.add (Hello2);        Sbset.add (HELLOSB);        Sbset.add (HELLO2SB);        Person Person1 = new person (1, "eke");        Person Person2 = new person (1, "eke");        Set personset = new HashSet ();        Personset.add (Person1);        Personset.add (Person2); Personwithhashcode code1 = new PersonwithhashCode (1, "eke");        Personwithhashcode Code2 = new Personwithhashcode (1, "eke");        Set codeset = new HashSet ();        Codeset.add (CODE1);        Codeset.add (CODE2);        System.out.println (Stringset.size ());        System.out.println (Sbset.size ());        System.out.println (Personset.size ());    System.out.println (Codeset.size ()); }

Operation Result:

Hello ' s HASHCODE:99162322HELLOSB's Hashcode:39219479hello2 ' s HASHCODE:99162322HELLO2SB's hashcode:2031787571====== true1221

  

The following are Personwithhashcode,person and Personwithhashcode just missing the Hashcode method.

The other thing about this code is that it reconstructs the Equals method, and the refactoring of this method is to follow a certain rule (the image comes from "Java Core Technology Volume II"):

public class Personwithhashcode {private int id;    private String name;        public personwithhashcode (int ID, String name) {this.id = ID;    THIS.name = name;    } public int getId () {return id;    } public void setId (int id) {this.id = ID;    } public String GetName () {return name;    } public void SetName (String name) {this.name = name;    } @Override public int hashcode () {return id * + Name.hashcode ();        } @Override public Boolean equals (Object object) {if (object = = this) return true;        if (object = = null) return false;        if (getclass () = Object.getclass ()) return false;        Personwithhashcode person = (Personwithhashcode) object;    return this.id = = person.id && Person.StringUtils.compare (name, Person.getname ()); } static class StringUtils {static Boolean compare (string A, string b) {if (a= = NULL && b = = null) return true;            if (a = = NULL && b! = null) {return false;            } if (A! = null && b = = null) return false;        return a.equals (b); }    }}

  

The role of Java Hashcode and the refactoring rules of the Equals method

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.