"Original" description of the Java object that needs to be overridden by the Equals method, the Hashcode method, the ToString method, the CompareTo () method

Source: Internet
Author: User
Tags comparable
In project development, we all have this experience, that is, when adding a table, the corresponding increase in Java classes, in the Java class, there are a number of common methods, including: Equals (), Hashcode (), toString (), CompareTo () These four methods, For beginners who just touch Java, for this piece can not be more in-depth understanding, just the simple meaning of copy and paste, and not very good to understand. Now borrow this time to talk about these methods. equals: used when comparing two objects equal (needs to be used with hashcode, explained in detail later) hashcode: The main purpose of overriding the Hashcode method is to allow hashtable/hashset/hashmap and other collections to function properly, that is, they are based on hashcode address judgments, and if you rewrite equals without rewriting hashcode, For HashMap to deposit 2 identical objects to save a key, but the corresponding 2 values, and take the time is unable to fetch the value, the other collection class is similar, in order to avoid confusion error, so the above collection class rewrite equals must override the Hashcode method. Is it the other class? I think not entirely, because the ordinary class re-hashcode meaning is not very big, do not write and will not appear compile errors. ToString: Because all classes are inherited from the Object,object class with a toString () method, each class that you create inherits the method. It returns a string representation of the object and is very helpful for debugging. However, the default ToString () method often does not meet the requirements, and this method needs to be overridden. In the end it is convenient for us to display. Compareto: Obviously, to compare the use of two objects, the comparator interface in Java comparable for the ordering of array objects, and to use this feature, you must have the class inherit from the comparable interface, overriding Compareto.

"Original" description of the Java object that needs to be overridden by the Equals method, the Hashcode method, the ToString method, the CompareTo () method

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.