课程: Java 8+ Essential Training: Objects and APIs

今天就学习课程吧!

今天就开通帐号,24,600 门业界名师课程任您挑!

Manage key-value pairs with Map

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…

内容