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. Advertising

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


    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. Advertising

  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. Bogi wrote:
    > 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?


    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. Andrea Desole wrote:
    >
    >
    > Bogi wrote:
    >
    >> 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?

    >
    >
    > 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?



    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

    On 11 Oct 2005 02:24:38 -0700, "Bogi" <> wrote or
    quoted :

    > 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);

    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.




    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 22, 2005
    #6
  7. Bogi

    Roedy Green Guest

    On Sat, 22 Oct 2005 04:10:05 GMT, Roedy Green
    <> wrote or quoted :

    >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.

    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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 22, 2005
    #7
  8. Bogi

    hoffmandirt

    Joined:
    May 13, 2008
    Messages:
    1
    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:
    <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:
    <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
    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. 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. Jon Rasmussen

    JSTL iterate array

    Jon Rasmussen, Jul 28, 2004, in forum: Java
    Replies:
    1
    Views:
    16,681
    John Fereira
    Jul 28, 2004
  2. Vince Darley
    Replies:
    4
    Views:
    4,564
    emilchacko
    Mar 2, 2010
  3. Robert Mark Bram

    Custom JSTL that accepts JSTL expression

    Robert Mark Bram, Dec 15, 2004, in forum: Java
    Replies:
    0
    Views:
    1,017
    Robert Mark Bram
    Dec 15, 2004
  4. Rakesh
    Replies:
    10
    Views:
    12,272
    Mike Schilling
    Apr 8, 2008
  5. DaFoot
    Replies:
    1
    Views:
    1,774
    DaFoot
    Oct 23, 2008
Loading...

Share This Page