Java Collections - ArrayLists and HashMaps

Introduction

ArrayList? HashMap? TreeSet? Ever seen these in Java code but didn't know what they were?

Java collections are important tools for storing more dynamic types of data. What if you don't know how much data there will be beforehand? What if the type of data you want to store isn't one of the basic types? What I mean here is, what if I want to store a bunch of Dog objects for example? Sure I could use an array but what if I don't know how many objects I'll have beforehand? What if I want to establish relationships between the data, linking say... a text string with a particular object?

We're going to take a brief and simple look into Java collections and learn what some of them are and how to use them.

ArrayList

Let's say you've created a simulation of the Earth, and you have the human population growing and growing. If Human is an object, how many Human objects will there be by the end of the simulation? What if you want to run the simulation forever? How can you possibly know how many Human objects there will be before creating your array to store them all? The truth is, you'd probably have to make your best guess. And as you may know, this is unacceptable.

Java has an incredibly useful object called the ArrayList. The Java ArrayList is basically a dynamic array. When you create this object, you don't specify a size for it at all. Let me make this perfectly clear: YOU DON'T NEED TO SPECIFY THE SIZE OF AN ARRAYLIST. That's right! This is an array that is initially completely empty.

What does an ArrayList look like? Well, it's sort of tricky. Because an ArrayList can store anything, we want to make sure to tell it exactly what we want it to store. Let's do a quick example and say we want the ArrayList to store Integers. Note: Integer and int are not the same. Integer is an object, int is a primitive type. Java can handle them similarly though.

You'll get a red line under the word ArrayList if you're using Eclipse. This is because you need to import ArrayList. This is the import statement:

So how do you add values to store? Here's how you add items to the end of an ArrayList:

Want to remove something from an ArrayList? No problem. Here's one of the ways to remove an item from an ArrayList. All you have to do is give the position in the array where the item is that you want removed.

This removes the 0th element in the array (that means the first element in Java, remember!). If the item was in the middle of the list, the list automatically shortens itself so there is no gap in your data. Pretty neat stuff huh?

How then can you access a particular element in the list? That too is simple:

As soon as I discovered array lists for myself, I almost completely stopped using regular arrays. That is how incredibly powerful these are!

HashMap

Java Collections are much more than just better arrays, however. Here we introduce the hash map. A hash map is a one-to-one relationship between one object and another. Let me explain it another way for those who don't want to remember math.

Let's say you want to access your email account. You can't do this right away though, so you're taken to a login screen. At this login screen you type in your username and password, and then when the combination is correct you can access your email account.

A hashmap works similarly. You have to give it an appropriate input so you can get the proper output. Each input is mapped to exactly one output. In our previous example, one combination of username and password produces exactly one email account. Here's a code example:

Again, you will be missing an import statement. In general you can get the import you need by typing:

import java.util.NameOfObject

where NameOfObject is the Collection you're using, in this case HashMap. You can also just hit Ctrl+Shift+O to automatically import everything in Eclipse.

If we look at the HashMap we just created, you will see <String, String>. This means that the first value (the key) is a String, and the second value is also a String. You can have any combination of objects, of course.

You use the key to obtain its value. One key corresponds to exactly one value, just like one username corresponds to one password.

Inserting new key/value pairs is simple:

And this is how you delete them:

If you want an array of all the keys in the hash map, you can use the keySet() method:

This is fairly tricky. The keySet method returns a Set. Then, that set is turned into an array of Strings by creating a new String that has a length that's the size of the HashMap. Then, that array is stored in an array called keys. The reason I'm creating an array of Strings is because the keys are all Strings from before.

How are hash maps useful? They let you store objects by looking up other objects instead of index values like you would with an ArrayList. Using the example above, if you needed to look up the password to someone's account, you could search the HashMap for the password by using the username as the key.

Note: I repeat, you CANNOT store basic (primitive) types inside of ArrayLists, HashMaps, or ANY Java collection for that matter. Java collections only store objects. This is EXTREMELY IMPORTANT so do not forget.

Hopefully this has provided enough of an introduction to using these advanced collection types. They are powerful tools and, when used appropriately, can greatly enhance your program's capabilities. There are far more than I have listed here; I strongly encourage you to look up the others, figure out how to use them, and then try to apply them into your own programs. Expect further examples of other things you can do with Java collections in the near future!

If you have any questions, comments, or concerns, feel free to contact us.

Custom Search