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