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;

    ....
    while(rs.next()){
    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);
    index++;
    }
    ....


    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}"/>
    </c:forEach>
    ....


    I trying for hours.
    I am really stuck here.


    Thank you in advance.
    Marc van den Bogaard
     
    Bogi, Sep 27, 2005
    #1
    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}"/>
    </c:forEach>

    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
    #2
    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:

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

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

    Thank you.
     
    Bogi, Oct 11, 2005
    #3
  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}/>
    </c:forEach>

    clear?
     
    Andrea Desole, Oct 11, 2005
    #4

  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
    #5
  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
    string.

    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
    #6
  7. Bogi

    Roedy Green Guest

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

    Let me quote from http://mindprod.com/hashcode.html#ONEKEY

    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
    #7
  8. Bogi

    hoffmandirt

    Joined:
    May 13, 2008
    Messages:
    1
    Likes Received:
    0
    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">
        <tr>
            <td>${mapEntry.value.filename}</td>
            <td>${mapEntry.value.contentType}</td>
            <td>${mapEntry.value.filesize} bytes</td>
            <td>${mapEntry.value.description}</td>
        </tr>
    </c:forEach>
    2. Use the varStatus attribute:

    Code (Text):
    <c:forEach items="${sessionScope.myFileUploadMap}" varStatus="status">
        <tr>
            <td>${status.current.value.filename}</td>
            <td>${status.current.value.contentType}</td>
            <td>${status.current.value.filesize} bytes</td>
            <td>${status.current.value.description}</td>
        </tr>
    </c:forEach>
    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).

    http://java.sun.com/javase/6/docs/api/java/util/Map.Entry.html.
     
    hoffmandirt, May 13, 2008
    #8
  9. Bogi

    tech_freaks

    Joined:
    Sep 2, 2009
    Messages:
    1
    Likes Received:
    0
    An article which explains JSTL for iterating Map can be viewed at tech-freaks.in/Java-Programming/Rapid-Code/iterate-map-jstl-el.html
     
    tech_freaks, Sep 2, 2009
    #9
    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.