+ Reply to Thread
Results 1 to 2 of 2

Thread: Java Maps II

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Java Maps II

    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:

    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);
            }
    This creates a random LinkedHashMap with 5 key-value pairs.

    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

    Code:
    Set<Integer> keys = theMap.keySet();
    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.

    We can get an iterator like this:

    Code:
    Iterator<Integer> it = keys.iterator();
    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:
    while (it.hasNext()) {
            System.out.println(it.next());
    }
    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.

    Value Set

    The values() method allows us to process the values associated with each key.

    Example:

    Code:
    Collection<Integer> values = theMap.values();
    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.

    Once again we can use an iterator to go through the values and see what they are.

    Code:
    Iterator<Integer> it = values.iterator();
    
            while (it.hasNext()) {
                System.out.println(it.next());
            }
    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.

    Example Output:

    24
    39
    45
    20
    39
    Entry Set

    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.


    Code:
    Set<Map.Entry<Integer,Integer>> entries = theMap.entrySet();
    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:
    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());
     }
    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().

    The above concepts are very useful and apply to all types of maps.

    Hope this helps!!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Java Maps II

    Nicely done! +rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. using tile maps
    By zapdude1234 in forum Java Help
    Replies: 5
    Last Post: 07-12-2011, 03:51 PM
  2. Maps
    By boosali in forum C# Programming
    Replies: 2
    Last Post: 07-19-2010, 05:11 AM
  3. Java Maps I
    By chili5 in forum Java Tutorials
    Replies: 3
    Last Post: 11-02-2009, 03:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts