More Java Collections – Meet Java Hashmap

Java HashMaps are neat. They are part of the Java Collections, and they are a kind of cousin to ArrayList, which we will use in the Note to Self project during the next chapter. They basically encapsulate useful data storage techniques that would otherwise be quite technical for us to code successfully for ourselves.

I thought it would be worth taking a first look at HashMap on its own.

Suppose we want to store the data of lots of characters from a role-playing game, and each different character is represented by an object of type Character.

We could use some of the Java tools we already know about, such as arrays or ArrayList. However, Java HashMap is also like these things, but with HashMap we can give a unique key/identifier to each Character object and access any such object using that key/identifier.

Note

The term "hash" comes from the process of turning our chosen key/identifier into something used internally by the HashMap class. The process is called hashing.

Any of our Character instances can then be accessed with our chosen key/identifier. A good candidate for a key/identifier in the Character class scenario would be the character's name.

Each key/identifier has a corresponding object; in this case, of the Character type. This is known as a key-value pair.

We just give HashMap a key and it gives us the corresponding object. There is no need to worry about which index we stored our characters—perhaps Geralt, Ciri, or Triss—at; just pass the name to HashMap and it will do the work for us.

Let's look at some examples. You don't need to type any of this code; just get familiar with how it works.

We can declare a new HashMap to hold keys and Character instances like this code:

Map<String, Character> characterMap;

The previous code assumes we have coded a class called Character.

We can initialize the HashMap as follows:

characterMap = new HashMap();

We can add a new key and its associated object like this:

characterMap.put("Geralt", new Character());

And this:

characterMap.put("Ciri", new Character());

And this:

characterMap.put("Triss", new Character());

Tip

All the example code assumes that we can somehow give the Character instances their unique properties to reflect their internal differences elsewhere.

We can then retrieve an entry from the HashMap as follows:

Character ciri = characterMap.get("Ciri");

Or perhaps use the Character class's methods directly, as follows:

characterMap.get("Geralt").drawSilverSword();

// Or maybe call some other hypothetical method
characterMap.get("Triss").openFastTravelPortal("Kaer Morhen");

The previous code calls the hypothetical methods drawSilverSword and openFastTravelPortal on the Character class.

Note

The HashMap class also has lots of useful methods like ArrayList. See the official Java page for HashMap here: https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html.

Now, let's talk about the Note to Self app.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.147.252