Iterating across a TreeMap....

Discussion in 'Java' started by Christian Williamson, Jun 8, 2008.

  1. I'd like to iterate over a TreeMap and print out the key and value for
    each entry, with each entry printout being on the same line. Anyone know
    how to do that?
     
    Christian Williamson, Jun 8, 2008
    #1
    1. Advertising

  2. Logan Shaw wrote:
    > Christian Williamson wrote:
    >> I'd like to iterate over a TreeMap and print out the key and value for
    >> each entry, with each entry printout being on the same line. Anyone know
    >> how to do that?

    >
    > Call entrySet() and iterate over that.


    Thanks, that's what I was going to do. The question then is: What do you
    print for each item in the entrySet? How do you obtain the key and value
    from an iterator? For example,

    //add key value pairs to TreeMap
    treeMap.put("2","Two");
    treeMap.put("1","One");
    treeMap.put("3","Three");
    ....
    Iterator i = treeMap.entrySet().iterator();

    while (i.hasNext()) {

    /* What goes here? Gotta have i.next(). Then what to print key
    and value? */

    }


    > According to the javadocs for
    > SortedMap (an interface which TreeMap implements), "order is reflected
    > when iterating over the sorted map's collection views (returned by the
    > entrySet, keySet and values methods)."
    >
    > - Logan
    >
    > P.S.: I'm responding only to the newsgroup, not to your e-mail address.
     
    Christian Williamson, Jun 8, 2008
    #2
    1. Advertising

  3. Christian Williamson

    Tom Anderson Guest

    On Sun, 8 Jun 2008, Christian Williamson wrote:

    > Logan Shaw wrote:
    >> Christian Williamson wrote:
    >>> I'd like to iterate over a TreeMap and print out the key and value for
    >>> each entry, with each entry printout being on the same line. Anyone know
    >>> how to do that?

    >>
    >> Call entrySet() and iterate over that.

    >
    > Thanks, that's what I was going to do. The question then is: What do you
    > print for each item in the entrySet? How do you obtain the key and value
    > from an iterator?


    Keep reading the javadoc.

    Seriously, find and read the documentation for Map.entrySet().

    tom

    --
    [Philosophy] is kind of like being driven behind the sofa by Dr Who -
    scary, but still entertaining. -- itchyfidget
     
    Tom Anderson, Jun 9, 2008
    #3
  4. Logan Shaw wrote:
    > Christian Williamson wrote:
    >> Logan Shaw wrote:
    >>> Christian Williamson wrote:
    >>>> I'd like to iterate over a TreeMap and print out the key and value for
    >>>> each entry, with each entry printout being on the same line. Anyone
    >>>> know
    >>>> how to do that?
    >>> Call entrySet() and iterate over that.

    >>
    >> Thanks, that's what I was going to do. The question then is: What do you
    >> print for each item in the entrySet? How do you obtain the key and value
    >> from an iterator? For example,
    >>
    >> //add key value pairs to TreeMap
    >> treeMap.put("2","Two");
    >> treeMap.put("1","One");
    >> treeMap.put("3","Three");
    >> ...
    >> Iterator i = treeMap.entrySet().iterator();
    >>
    >> while (i.hasNext()) {
    >>
    >> /* What goes here? Gotta have i.next(). Then what to print key
    >> and value? */

    >
    > Map.Entry entry = (Map.Entry) i.next();
    > System.out.println("key is " + entry.getKey()
    > + " and value is " + entry.getValue());
    >
    > The above assumes you're not using generics. I would probably do it
    > with generics, though:
    >
    > SortedMap<String,String> map = new TreeMap<String,String>();
    >
    > map.put("2", "Two");
    > map.put("1", "One");
    > map.put("3", "Three");
    >
    > for (Map.Entry<String,String> entry : map.entrySet()) {
    > System.out.println("key is " + entry.getKey()
    > + " and value is " + entry.getValue());
    > }
    >
    > The above code is untested, but I'm fairly sure it's right.
    >
    > - Logan
    >


    Excellent, thanks. Yep, it worked.

    import java.util.Collection;
    import java.util.TreeMap;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.Map;
    import java.util.SortedMap;


    public class IterateExample {

    SortedMap<String,String> map = new TreeMap<String,String>();

    public static void main (String [] args) {
    IterateExample ie = new IterateExample();

    ie.map.put("2", "Two");
    ie.map.put("1", "One");
    ie.map.put("3", "Three");

    for (Map.Entry<String,String> entry : ie.map.entrySet()) {
    System.out.println("key is " + entry.getKey()
    + " and value is " + entry.getValue());
    }
    }
    }
     
    Christian Williamson, Jun 9, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa

    HashMap vs TreeMap

    Ahmed Moustafa, Aug 9, 2003, in forum: Java
    Replies:
    2
    Views:
    46,438
    Roedy Green
    Aug 10, 2003
  2. Harry

    TreeMap problem...

    Harry, Sep 22, 2003, in forum: Java
    Replies:
    3
    Views:
    475
    Harry
    Sep 23, 2003
  3. Matthew Wilson

    iterating bit-by-bit across int?

    Matthew Wilson, Oct 23, 2003, in forum: Python
    Replies:
    12
    Views:
    537
    Brian Kelley
    Oct 25, 2003
  4. Drew
    Replies:
    18
    Views:
    421
    Bruno Desthuilliers
    Mar 14, 2007
  5. carl
    Replies:
    5
    Views:
    2,392
    James Kanze
    Nov 25, 2009
Loading...

Share This Page