In the ArrayList
chapter, you learned that Arrays store items as an ordered collection, and you have to access them with an index number (int
type). A HashMap
however, store items in "key/value" pairs, and you can access them by an index of another type (e.g. a String
).
One object is used as a key (index) to another object (value). It can store different types: String
keys and Integer
values, or the same type, like: String
keys and String
values:
Example
Create a HashMap
object called capitalCities that will store String
keys and String
values:
import java.util.HashMap; // import the HashMap class
HashMap<String, String> capitalCities = new HashMap<String, String>();
Practice Excercise Practice now