From the course: Java 11+ Essential Training

Unlock the full course today

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

Manage key-value pairs with Maps

Manage key-value pairs with Maps - Java Tutorial

From the course: Java 11+ Essential Training

Start my 1-month free trial

Manage key-value pairs with Maps

- [Instructor} I've previously described how to create two types of ordered collections: a simple array and a list. Now I'm going to show you how to create an unordered collection where each item in the collection is a key-value pair. These are called maps in Java. Map is an interface just like this is an interface, and there are concrete classes you can choose from that implement the methods defined by the Map interface. I'll start here by creating an instance of a Map. When you declare a Map, you can use generic notation to indicate the type of both the key and the value. In some languages, key-value pairs can only use strings for the keys. In Java, you can use any kind of Object. I'll set the key as a String and the item type as ClothingItem. I'll name this Map items, and I'll initialize it with new, and then I'm going to choose a Map class called HashMap. Just as I showed with the List interface and the concrete…

Contents