Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Most programming languages have the ability to store unordered collections of data where you can randomly access items in the collection by their keys. In some languages these are known as associative arrays, in others structures. But they always serve the same purpose, an unordered collection of data where you can get to one item at a time. In Java, we do this with a class called the HashMap, just like the array list it's a class. And when you declare it you have to import it. I'll start in this empty main method in the project HashMap and I'll declare an instance of the HashMap class.
I'll type in the name of the class and press control space. Eclipse recognizes that this class must be imported and it adds the import at the top of the code. Just as with the array list the HashMap is designed to store data all of the same type but where the array list means one data type, the HashMap needs two. Each item in a HashMap will have a key and a value. Notice that these are represented in the Diamond Operator that is where the generic is declared using the letters K and V.
In some languages the key always has to be a string, but in Java you can really use any object. For simplicity though in this exercise I'll declare both my keys and my values to be strings. So I'll type in String, press tab and type String again and then I'll declare the variable name, map. Next, I'll instantiate the class. I'll type in the code = new then once again I'll type the name of the class and press control space. There are four different versions of the constructor method for HashMap.
There is one with no arguments, there is one with an initial capacity, and there are a couple that let you initialize the object in other ways. I'm going to choose the simplest the one with no arguments. Eclipse looks at my original declaration and matches my constructor method call. Now the codes get in little wide so I'll maximize my editor. I'll add the semicolon at the end of the line and now I've created my first HashMap. In order to add items to the HashMap use the method named put.
I'll call map.put and I'll see that there's only one version of the put method. It requires two values, in this case two strings. And once again using the names of states, I am going to use the name of the state as the key and its capital as the value. So I'll type in California then tab over and type in Sacramento. I'll add another item to the map this will have a state name of Oregon and a capital of Salem and finally I'll add Washington and Olympia.
Now I'd like to output the contents of the map to the command line. Just as with the array list, the HashMap has a two string method that will serialize its contents so it's easy to read. So I'll use System.out.println and just pass in the map object. I'll save and run my application and there is the results. Each item contains a key and a value separated by the equals operator and the entire list is wrapped in curly braces.
Just as with array lists, HashMaps are resizable at runtime. So I'll add in yet another State Alaska and its capital Juneau and I'll copy and paste my printline command. I'll organize my codes, so that it's all grouped together correctly, and I'll run the application and there you see that I am successfully adding an item. You can find an item in a map by referring to the key.
So for example, let's say that I wanted to find the capital of Oregon. I'll declare a string variable, I'll call it cap and I'll call map.get and I will pass in the key Oregon. And then I'll use of printline command and I'll say the capitol of Oregon is and I'll output the value of cap. And I'll run the code and I get the capitol of Oregon is Salem, that's correct.
Now once again as I mentioned this second item in the HashMap does not have to be a string, it could be any kind of complex object. And as you get into creating your own custom classes that represent data, you'll find that HashMaps are very useful for storing large collections of data in memory. Next, I am going to remove an item. With the array list you removed an item by its index position, but remember a HashMap isn't in any particular order. So if you want to remove an item, you refer to its key.
So I'll call map.remove and I'll pass in California and I'll print out the map, and I've removed California and I'm left with the three remaining states. Take a look at the documentation for the HashMap class to learn more about it. I'll select the name of the class and go to Dynamic Help, click for the javadoc and maximize and go to the method list. You'll see that there are methods to determine whether a particular item is available in the list. Those methods are name contains key and contains value.
There is a method for determining whether the HashMap is empty, it's called isEmpty and returns a Boolean. There's put methods, a putAll method, size methods and values methods. I'll show you how to use a couple of these other methods in a later video when I show you how to loop through the HashMap using something called an iterator object. But this should give you enough information to get started with HashMaps storing unordered data collections in your Java applications.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98974 Viewers
61 Video lessons · 86265 Viewers
71 Video lessons · 70107 Viewers
56 Video lessons · 102324 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.