From the course: Java: Advanced Concepts for High-Performance Development

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Storing pairs with HashMaps

Storing pairs with HashMaps

- [Instructor] Sometimes you'll need a data structure that can store key-value pairs. Probably the most common way to do this in Java is with a HashMap. So in this example I have a class called HashmapExample and at the moment this just has an empty main method inside it. So to create a HashMap, I'm going to put the word Hashmap and IntelliJ has added the import statement at the top for me. And then inside the pair of angle brackets, I need to put two types. The first type is going to be the type of the key and the second type is the type of the value associated with that key. So let's say for example, I want to create a HashMap that represents a shopping basket in a grocery store. So the key is going to be the name of the product being scanned and the value is going to be the number of times that item has been scanned. So for example, if someone scans one apple, the key will be apple and the value will be one. Then…

Contents