1. I can use a HashMap to store data with key and value pairs.
A HashMap is a collection that uses key and value pairs to store data. The key is a reference for access to the value; kind of like an index, except we can go beyond using numbers. A value is the actual data stored.
Think of a HashMap as a dictionary. When you look in a dictionary, you look up a key, or the word you want to know the definition for. The word is the key, the definition is the value.
Except in a HashMap we can use different data types for keys or values. We no longer need to use an index. We can set your name to be the key and you birthday to be the value, etc.
HashMaps cannot have duplicate keys, but they can have duplicate values.
ArrayLists are very similar to HashMaps and you’ll notice that they are instantiated almost the same way.
An ArrayList uses an index to refer to a value that is really stored in an array.
A HashMap uses an internal node structure. Nodes are really powerful but exceed the scope of this class.
The order of a HashMap is not guaranteed. For instance, the first key/value pair you put in the map, will not always be the first in order.
1. import HashMap from the java.util package.
2. Instantiate a HashMap and pass two data types separated by a comma in between the angle brackets <>. The first type represents the key data type, the second type represents the value data type.
1. put(K key, V value);
The put method will put a new key and value pair in your hash map. The first parameter is the key that you'll use to reference the value.
2. remove(Object key);
The remove method will remove a key value pair by sending the key you want to remove.
3. size();
The size method will return the size of the HashMap (How many key/value pairs you have).
4. get(Object key);
The get method will return a value, given a key.
5. keySet();
The keySet method will return all the keys in a HashMap. (Not in a specific order).
6. values();
The values method will return all the values in a HashMap. (Not in a specific order).
These are some more HashMap methods that you will find useful! There are more than this, but these are the next set of very useful ones!
1. clear();
will completely clear the entire HashMap of all keys/values.
2. containsKey(Object key);
Will return true if a key is in the HashMap, false if it isn't.
3. containsValue(Object value);
Will return true if the value is in the HashMap, false if it isn't.
4. putIfAbsent(K key, V value);
Will only put the key and value pair if it is not there.
5. replace(K key, V value);
Will replace the entry for this key if it is already mapped.
Filling a HashMap is similar to ArrayLists except we now need to ask for two details. An input for a key, and then what value they want stored for that key.
Iterating a HashMap requires using a for each loop that loops over all the possible keys. (Example in the gist to the right!)
1. Create a HashMap for a restaurant menu.
2. Key -> String (which will be the name of the item)
3. Value -> String (which will be the description of the food with ingredients)
4. Let the user fill the HashMap with food and descriptions.
5. If they say stop, it should exit and print out the menu!