Difference between revisions of "Generics"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 1: Line 1:
OCPJP Java Generics
+
'''Collections and Generics'''
  
<syntaxhighlight lang="java">
+
<u>equals() and hashcode() </u>
  
public static void main(String[] args) {
+
* Mainly used for storing and retrieving objects from hashed collections.
      System.out.println("Hello World");
+
* First object's hashcode is used to figure out which hash bucket the object is in
}
+
* Then the equals() method is used to compare objects in the same hash bucket.
  
</syntaxhighlight>
+
This means that:
 +
* equal objects MUST have the same hashcode
 +
* unequal objects can have the same (or different) hashcode (e.g. same bucket but objects are different)
 +
* objects having different hashcodes MUST be unequal
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Revision as of 23:45, 23 May 2011

Collections and Generics

equals() and hashcode()

  • Mainly used for storing and retrieving objects from hashed collections.
  • First object's hashcode is used to figure out which hash bucket the object is in
  • Then the equals() method is used to compare objects in the same hash bucket.

This means that:

  • equal objects MUST have the same hashcode
  • unequal objects can have the same (or different) hashcode (e.g. same bucket but objects are different)
  • objects having different hashcodes MUST be unequal