Skip to content

Map and HashMap

miruaraya edited this page Mar 25, 2024 · 1 revision

The HashMap class provides basic implementation of the Map interface, used to map key-pair values. While Arrays store values in an ordered collection and must be accessed with an index number a HashMap stores items in key-value pairs that are accessed by an index of another type. One object is used as a key or index for another object or value, which can be stored in different or the same type.

Relevant methods include: "put()" to add items by key and value, "get()" to access an item referring to its key, "remove()" to clear a specific item by its key, and "clear()" to remove all items. Items of a HashMap can be iterated through by a for each loop with the "keySet()" method to return keys and "values()" for values. Keys and values are all objects and to use primitive types, like "int", and equivalent wrapper class must be specified (Integer) or Boolean for boolean, Character for char, etc.

Examples here: W3 Schools Page on HashMap

Clone this wiki locally