-1

I have stored the below values in a hashmap which is like this -

a1 - name
b1 - age
c1 - city
a2 - name1
b2 - age1
c2 - city1
...

I am fetching these values from the db. I am not able to figure out how to iterate through this key-value pair and display on a table in a jsp page in a particular order. I need to display it like this in the table

name age city name1 age1 city1 name2 age2 city2 . . .

Please let me know how to do this.

Regards Siddharth

siddarth v
  • 1
  • 1
  • 2
  • hava a look on http://stackoverflow.com/questions/1066589/java-iterate-through-hashmap – manivannan Aug 29 '14 at 12:54
  • https://jstl.java.net/. – Aleksandr M Aug 29 '14 at 12:58
  • you can use , look at: http://stackoverflow.com/questions/6164560 – Benjamin Aug 29 '14 at 13:14
  • @Benjamin Before the op can use jstl, he has to import the jstl core libraries into the jsp files. – Reporter Aug 29 '14 at 13:24
  • @reporter yes, by writing something like: `<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>` – Benjamin Aug 29 '14 at 13:28
  • Thanks for replying everyone. I am not sure if I made myself clear in the question. Iterating through the hasmap key-value set is not an issue. I need to display it in a certain order depending on the number of rows I retrieve from the db. Hashmap key-value set will contain each row of the db (a1-name, b1-age,a2-name,b2-age). Say the db contains 10 rows,I need to display all 10 rows with name,age etc. I know I can use lists to do it, but I have a constraint to only use hashmaps. Please let me know if there is a way to do this – siddarth v Aug 29 '14 at 16:24

2 Answers2

0

Use <c:forEach items="${map}" var="mapEntry" varStatus="loop">

and ${mapEntry.value} to get the map values inside the loop.

JavaLearner
  • 389
  • 1
  • 3
  • 8
  • I managed to do it. This is what I did. Thanks for all your replies- int no_of_records = ((int) valueMap.get("no_of_records")); for (int i =1;i
    <%= (String) valueMap.get("a"+i) %> <%= (String) valueMap.get("b"+i) %> <%= (String) valueMap.get("c"+i) %>
    – siddarth v Sep 01 '14 at 10:06
0

Try like this

<c:forEach items="${map}" var="entry">
    <tr>
        <td>${entry.key}</td>
        <td>${entry.value}</td>
    </tr>
</c:forEach>

Don't forget to include this

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • Thanks for your reply. But I need to display it like this in a table (First row - name, age, city, Second Row - name1,age1,city1). Will this work in this case? – siddarth v Aug 29 '14 at 14:11
  • What tr and td means to you – SparkOn Aug 29 '14 at 14:12
  • Hashmap key-value set will contain each row of the db (a1-name, b1-age,a2-name,b2-age). Say the db contains 10 rows,I need to display all 10 rows with name,age etc. I know I can use lists to do it, but I have a constraint to only use hashmaps. Please let me know if there is a way to do this – siddarth v Aug 29 '14 at 16:21