I spent a lot of time trying to come up with a good metaphor for this and settled on a Wall Poster Display to describe a list and then "upgrade" the analogy to turn it into a hashmap/set. Let me know what you think about this analogy and if my article is easy to understand. Thanks!
Nice post. I could understand how the hashcodes working. I have bump. According to your analogues, will different kinds of thing can appear in a one frame. Something like, one frame contains game posters and movie poster? Could you please explain it relate to Java ?
OCAJP Java 8, OCPJP Java 8
posted 5 years ago
Yes, that is possible but it has more to do with Java and its object oriented capabilities than with hashcode/HashSet.
As an example, you could have a Poster class and a Game Poster class that extends Poster. Then you could have a Movie Poster class that also extends Poster. Now, if you have a HashSet that takes a list of Posters, you can put both Movie Posters and Game Posters in it. You can also create a HashSet that only lets you pass in Game Posters. If you tried to put a Movie Poster into it, your code will error when you try to compile.