Iterate over Hashmap with JSTL

Discussion in 'Java' started by Bogi, Sep 27, 2005.

  1. Bogi

    Bogi Guest

    Hi all!

    I've a HashMap construct like this:

    HashMap all = new HashMap();
    int index=0;

    ResultSetMetaData rsmd = rs.getMetaData();
    HashMap h = new HashMap();

    for(int i=1;i<=rsmd.getColumnCount();i++){
    String col=rsmd.getColumnName(i);
    h.put(col, rs.getString(col));

    all.put(String.valueOf(index), h);

    I want to know how to iterate over the rows, so that I can retrieve
    the values like

    <c:forEach var="loop" items="${hashmap}" varStatus="status">
    <c:eek:ut value="${loop.columnName1}"/>
    <c:eek:ut value="${loop.columnName2}"/>

    I trying for hours.
    I am really stuck here.

    Thank you in advance.
    Marc van den Bogaard
    Bogi, Sep 27, 2005
    1. Advertisements

  2. I don't think you can do this directly. What you can do is to iterate
    over the set of the values (given by the values() method), doing
    something like:

    <c:forEach var="loop" items="${hashmap.values}" varStatus="status">
    <c:eek:ut value="${loop.columnName1}"/>
    <c:eek:ut value="${loop.columnName2}"/>

    of course, because you don't have a getter for values, you will have to
    implement a small wrapper, or a derived class.

    Also, keep in mind that the values in the map are not sorted, so maybe
    this is not really what you want
    Andrea Desole, Sep 27, 2005
    1. Advertisements

  3. Bogi

    Bogi Guest

    Hello again,

    I finally changed my code to:

    HashMap first = new HashMap();
    first.put("longname", "Deutsch");
    first.put("shortname", "de");

    HashMap second = new HashMap();
    second.put("longname", "Englisch");
    second.put("shortname", "en");

    HashMap h = new HashMap();
    h.put("0", first);
    h.put("1", second);

    request.setAttribute("hashmap", h);

    But I got the same problem.

    I just want to know how to iterate over a nested HashMap and access the
    key, e.g. in for my source:

    <c:eek:ut hashmap.longname>
    <c:eek:ut hashmap.shortname>

    Does anyone has an example for me?
    It would be great.

    Thank you.
    Bogi, Oct 11, 2005
  4. okay, without writing a complete example I'll try to be a bit clearer.
    your variable h shouldn't be a map, it should be a vector:

    Vector h = new Vector()
    h.add( first );
    h.add( second );
    request.setAttribute("vector", h);

    then you can iterate over your vector, and read your maps:

    <c:forEach var="map" items="${request.vector}">
    <c:eek:ut value="${map.longname}/>
    <c:eek:ut value="${map.shortname}/>

    Andrea Desole, Oct 11, 2005

  5. Now I got it!

    I used the logic:iterate tag instead of c:forEach and it works with the
    example above.

    Thank you!
    Marc van den Bogaard, Oct 11, 2005
  6. Bogi

    Roedy Green Guest

    If this is your actual application, you don't need the complexity of
    a nested HashMap or dynamically named fields. You just need a Language
    class with three fields:

    id, longname, shortname.

    you can then build a HashMap on those objects keyed by either id,
    longname, or shortname. to get the Language object. From there you can
    at all three values.

    If you want "00" to be treated as "0" then store you id as a int not a

    You can define only one hashCode/equals method for your object. That
    limits you to one type of lookup for your objects. There is no
    equivalent to Comparator for HashMaps.
    Roedy Green, Oct 22, 2005
  7. Bogi

    Roedy Green Guest

    cancel cancel! I am drooling here confusing HashSets and HashMaps.

    Let me quote from

    The One Key Catch

    You can define only one hashCode/equals method for your HashSet
    Objects. That limits you to one type of HashSet lookup for your
    Objects. There is no equivalent to Comparator for HashSets. You can
    look up your Objects by only one key, though that key might contain
    several fields. You can't have several HashSets each accessing the
    same Objects by different keys. You can of course have several
    HashSets each accessing a different subset of the same group of
    Objects using the same key.

    In contrast, with HashMap you have more freedom. Your Objects don't
    have to implement a useful hashCode/equals, but any keys you use do.
    Since you can define different hashCode/equals for different types of
    key, you can have multiple HashMaps on the same group of Objects
    looking up by different keys.
    Roedy Green, Oct 22, 2005
  8. Bogi


    May 13, 2008
    Likes Received:
    Iterate over Map using JSTL

    I know this is a late post, but it took me a while to figure this out and I didn't find an answer here or elsewhere, so I'm posting this to hopefully save other people time. I found two very similar ways to iterate over a Map using JSTL:

    1. Use the var attribute:

    Code (Text):
    <c:forEach items="${sessionScope.myFileUploadMap}" var="mapEntry">
            <td>${mapEntry.value.filesize} bytes</td>
    2. Use the varStatus attribute:

    Code (Text):
    <c:forEach items="${sessionScope.myFileUploadMap}" varStatus="status">
            <td>${status.current.value.filesize} bytes</td>
    Basically, when you iterate over a Map using JSTL, you are iterating over Map.Entry objects and NOT whatever you have placed in the value field. Therefore you need to call getValue (.value above). You can also call getKey (this would be .key above).
    hoffmandirt, May 13, 2008
  9. Bogi


    Sep 2, 2009
    Likes Received:
    An article which explains JSTL for iterating Map can be viewed at
    tech_freaks, Sep 2, 2009
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.