Maps
A map is a collection that maintains a mapping of a key to a value. The implementations of the Map interface are generic classes. This means that the key and the values must be objects.
One use of a map would be to keep track of how many words occur in a sentence.
There are three main implementations of the map class.
- HashMap
- Hashtable
- LinkedHashMap
- TreeMap
Creating a Map
When we create a map we must specify the type of map, and the type parameters of the key and the type parameter of the value.
Example:
Notice how I declare the type as Map and not HashMap? This Map is an interface that is implemented by HashMap. This makes it much easier to change the implementation later.Code:Map<String, Integer> theMap = new HashMap<String, Integer>();
I would just change HashMap to LinkedHashMap and the program would not break.
Example of creating a LinkedHashMap
Differences Between MapsCode:Map<String, Integer> theMap = new LinkedHashMap<String, Integer>();
The classes HashMap and Hashtable both implemented unordered maps. The big difference between the HashMap class and the Hashtable class is that the Hashtable class does not permit null keys and values. The HashMap class does.
A LinkedHashMap maintains items in insertion order. So when you iterate through the map, you will access items in the order that you added them to the map.
A TreeMap implements a SortedMap interface. This map maintains items in sorted order.
Adding Items
Despite what method you use to add items, the method is always the same to add items.
You specify the key and the value that the key should be mapped to. The key might be the word and the value might be the number of times that key has occured.Code:Object put(Object key, Object value)
Notice that you can only add each key once. If I try to add James again, it will just update the value previously associated with James.Code:theMap.put("James",new Integer(1));
Example:
James is now associated with a value of 2 not 1.Code:theMap.put("James",new Integer(2));
Removing Items
This is as simple as calling the remove function and passing the key that you want to remove.
Example:
Now, the program is free to add James as a key again. This function also returns the value that was associated with the key. So if James was associated with a 2, the return value would be an Integer object holding the value 2.Code:theMap.remove("James");
Otherwise, the program returns null.
Retrieving Values
This is as easy as using the get method and passing in the key. So if I want to know the frequency that James has occured at I would call this method:
James is the key and the return value is the value associated with the key. In this case, the frequency that James has occured at.Code:theMap.get("James");
Contains Key
If we want to check if a certain word has occured in the sentence, we would use the containsKey method. This method returns true if a certain key exists.
Example:
There are more methods available to you, but these are the majority of the useful ones. We will look at iterating through the maps in the next tutorial.Code:if (theMap.containsKey("James")) { System.out.println("James has occured in the sentence."); }
Exercise
Create a word counter. The user will enter a sentence using standard input. The program will keep track of how many times each word occurs in a sentence.
Output all the words that occured and the frequency at which they occured.
Maps seem a lot like arrays! Very cool though. +rep!
Kinda but the key difference is you can use anything you want as a key.
It is a lot like the associative arrays in PHP.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks