Home Ask Login Register

Developers Planet

Your answer is one click away!

Joe February 2016

how to populate html table with hashmap k,v?

I have a hashmap with k v, and there are some dup keys there, like:

123 foo

123 goo

345 ggg

567 kkk

I want to populate my html table with this info, even if there is duplicates, so this is how I could just print it:

for (Map.Entry<String, List<String>> entry : total.entrySet()) {
  for (String s : entry.getValue()) {
    System.out.println(entry.getKey() + " " + s);

so now how can I populate the table, i tried something like this:

<table id="ptable" border="1">

                    <td style="text-align: center;">ID</td>
                    <td style="text-align: center;">Month</td>

and then:

< c:forEach var="employeeHash" items="${employeeHash}" >

but I dont know how to get to the value for each key...

i want the final res to look like:

key val

123 kkk

123 fff

345 lll


Sheetal Mohan Sharma February 2016

Assuming your employeeObjMap has key and values. Following code should work.

  <c:forEach items="${employeeObjMap }" var="current">
      <td><c:out value="${current.key}" /><td>
      <td><c:out value="${current.value}" /><td>

Emidio Dias February 2016

You can say that you also want the value, like that:

<c:forEach var="employeeHash" items="${employeeHash}" >

Take a look into this link: Use <c:forEach> with HashMap

Arnaud Denoyelle February 2016

This should be a comment but I do not have enough place. Please do not vote.

I am not a JSP expert but nesting 2 foreach should do the trick :

<c:forEach var="entry" items="${map}" >
  <!-- entry.key is employee.key -->
  <!-- entry.value is employee.skills -->
  <c:forEach var="skillId" items="${entry.value}" >

Post Status

Asked in February 2016
Viewed 2,679 times
Voted 8
Answered 3 times


Leave an answer

Quote of the day: live life