From the course: Advanced Java Programming

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Storing pairs with HashMaps

Storing pairs with HashMaps - Java Tutorial

From the course: Advanced Java Programming

Start my 1-month free trial

Storing pairs with HashMaps

- [Instructor] Hashmaps are a common data structure used in Java programing. They allow you to store pairs of values together. One example use for a hashmap is a phone book. I could create a hashmap where the name of the contacts was the key, and the phone number was the value. To do this, in my main method in the hashmap example class I will create a hashmap variable. Inside the angle brackets after hashmap, I will specify that the key should be of type string and the value should be of type integer. (keyboard clicks) I will call the variable phone book. (keyboard clicks) And I will assign it to a new instance of hashmap. (keyboard clicks) To add values to the hashmap, I'll use the put method. For example, I can do phonebook.put and parse in Kevin as the key, and then parse in an integer such as 12345 as the phone number. (keyboard clicks) I can then add as many values as I like. I will add a contact called Jill who's number is 98765, (keyboard clicks) and a contact called Brenda…

Contents