Learn Java Programming - Overriding the .hashCode() method Tutorial

Pubblicato il: 14 gennaio 2016
sul canale di: Daniel Ross
2,240
27

The Object class contains eleven methods; over half of those methods are somehow dependent upon other methods within the Object class to function properly. In a previous tutorial I discussed the relationship between the .wait() method and the .notify() and .notifyAll() methods. One of the most widely used methods, .equals(), shares a special relationship with the .hashcode() method. At this point you should already be familiar with invoking the .equals() method on an object. The purpose of this tutorial is to explain what a hash code is and how to properly override the .hashCode() method.

So what is a hash code? A hash code is a number, and in Java it can simply be any number that fits into the primitive int range of -2147483648 to 2147483647.
What purpose does a hash code serve? The purpose of a hash code is to provide a quick way of locating an object when it is mixed in with a bunch of other objects (clear as mud right???).
Without realizing it, we use hash codes all over the place in everyday life. Imagine that you have a craving for a can of Coca-Cola (Coke) so you head over the grocery store to get one. When you step into the grocery store you are presented with a bunch of stuff for sale. That "stuff" is comprised of thousands of objects; a six-pack of Coke is an object and it the specific object that you are searching for amongst a sea of other objects.
So where to we begin? We'll just start walking around hoping to stumble across it and we will locate it eventually after some time has elapsed.


In questa pagina del sito puoi guardare il video online Learn Java Programming - Overriding the .hashCode() method Tutorial della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Daniel Ross 14 gennaio 2016, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 2,240 volte e gli è piaciuto 27 spettatori. Buona visione!