From the course: Java 8+ Essential Training: Objects and APIs
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Manage key-value pairs with Map - Java Tutorial
From the course: Java 8+ Essential Training: Objects and APIs
Manage key-value pairs with Map
- [Instructor] I've previously described how to use a list to manage ordered data. The list was the interface, the contract that defined a set of methods, but I used a concrete class named array list that kept the data in a particular order. Now I'll show you how to use a class named HashMap to represent an unordered data collection, containing key value pairs. Just as with an array list, which implements the list interface, a HashMap is an implementation of an interface named map. I'll start here in my empty main method, and I'll declare an instance of the map interface. Maps use generic declarations, just like lists, but the items within a map are key value pairs. Both the key and the associated value can be of any class type. So for example, if I want the keys to be strings, I would pass in string as the first type, and if I want the associated values to be strings, I would pass that in again. I'll name my object map, and I'll instantiate it with new HashMap, just like with lists…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.