Map Views
In the last tutorial, we looked at creating maps. In this tutorial we are going to look at how to output maps. There are three different views that we might be interested in.
They are the keys, the values, and both the keys and the values.
First we need to create a map. Run this code:
This creates a random LinkedHashMap with 5 key-value pairs.Code:Map<Integer, Integer> theMap = new LinkedHashMap<Integer, Integer>(); int nKey, nValue; for (int i = 0; i < 5; i++) { nKey = (int)(Math.random()*50)+10; nValue = (int)(Math.random()*30)+20; theMap.put(nKey,nValue); }
We can't explicity iterate the map but we can create set views that we can iterate through.
The three methods that we are interested in are:
- Set keySet()
- Collection values()
- Set entrySet()
Each of these methods returns a Collection that we can iterate through. An important feature to note is that if the set is modified then the map is also modified. The values method returns a Collection and not a set becaues there might be multiple values where as a Set only allows unique items.
Key Set
This function returns a set that maintains all of the keys. We can now get an iterator that allows us to look at each key. Since we are using a LinkedHashMap the items we will access our in insertion order. The first item added is the first item accessed with the iterator.Code:Set<Integer> keys = theMap.keySet();
We can get an iterator like this:
Now, it is simply a matter of using a loop with the hasNext and next methods to go through the keys. The hasNext method will return false when the iterator has looked at each item. The next method returns the current item being pointed at and then moves to the next item.Code:Iterator<Integer> it = keys.iterator();
That is simply. In that loop you can do all the processing of the keys that you want. You could also change the key which would be reflected in the map.Code:while (it.hasNext()) { System.out.println(it.next()); }
Value Set
The values() method allows us to process the values associated with each key.
Example:
This values method returns a list of all the values in the map. It has to be a list because a set wouldn't allow duplicate values. A map can have duplicate values.Code:Collection<Integer> values = theMap.values();
Once again we can use an iterator to go through the values and see what they are.
This code is the exact same code as above. What you will get as output is all the values that are held in the map.Code:Iterator<Integer> it = values.iterator(); while (it.hasNext()) { System.out.println(it.next()); }
Example Output:
Entry Set24
39
45
20
39
This is probably the more useful function. When you want to know what the value is and what key is associated with that value.
Perhaps, you want to know what word occurs with what frequency. Another useful situtation is you want to know what nodes a certain node is adjacent to in a graph. A map would be great for this.
Getting an Entry Set
The entrySet method returns a set holding Map.Entry objects. Each Map.Entry object contains a getKey method, a getValue method and a setValue method. Using the setValue method makes changes in the map. These changes are reflected in the map.
We now have a set that holds a bunch of Map.Entry objects. We can once again get an iterator to process the items in the set.Code:Set<Map.Entry<Integer,Integer>> entries = theMap.entrySet();
Very simple code. Notice though that we have to store the next entry in a temporary object. Why? Well if we call it.next().getKey() the iterator moves to the next item so we cannot call it.next().getValue().Code:Iterator<Map.Entry<Integer,Integer>> it = entries.iterator(); while (it.hasNext()) { Map.Entry<Integer,Integer> next = it.next(); System.out.println(next.getKey() + "->" + next.getValue()); }
The above concepts are very useful and apply to all types of maps.
Hope this helps!!
Nicely done! +rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks