Java Week Eighth job

Source: Internet
Author: User

Analysis Code:

public final class Lineitemkey implements Serializable {private integer customerorder;private int itemid;public lineitemkey () {}public Lineitemkey (Integer order, int itemId) {this.setcustomerorder (order); This.setitemid (itemId);} @Overridepublic int hashcode () {return (This.getcustomerorder () = = null? 0:this.getcustomerorder (). Hashcode ()) ^ ((int ) (This.getitemid ()));} @Overridepublic Boolean equals (Object otherob) {if (this = = Otherob) {return true;} if (! ( Otherob instanceof Lineitemkey) {return false;} Lineitemkey other = (Lineitemkey) Otherob;return ((this.getcustomerorder () = = null? Other.getcustomerorder () = = Null:thi S.getcustomerorder (). Equals (Other.getcustomerorder ())) && (this.getitemid () = = Other.getitemid ()));} @Overridepublic String toString () {return "" + getcustomerorder () + "-" + Getitemid ();} /* Getters and Setters */}  

The role of Hashcode is:

When we add an element to a collection, the collection calls the Hashcode method first, so that it can directly locate where it is stored, and if there are no other elements, it is saved directly. If there is already an element in place, call the Equals method to match whether the two elements are the same, the same is not present, and the difference is hashed to another location. This processing, when we deposit a large number of elements, can greatly reduce the number of calls to the Equals () method, greatly improving the efficiency.

So Hashcode's role in the above is to seek the domain (Find an object in the collection region position). Hashcode can be divided into a number of areas, each object can calculate their hash code, hash code can be grouped, each grouping corresponding to a storage area, according to an object's hash code can determine the object's storage area, so that the number of query matching elements greatly reduced, Improved query efficiency. This is because CustomerOrder and itemid together form a composite key to mark an entity, so if CustomerOrder does not exist then returns 0, if present, returns the value of the CustomerOrder hash value ^itemid.

The role of Equal:

Whether the two variables represented by the equals operation are references to the same object, that is, whether the contents of the heap are the same. Here is a comparison of whether two objects are the same, returns true if they are the same, and returns false if they are not.

Java Week Eighth job

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: 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.