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

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

and then:

< c:forEach var="employeeHash" items="${employeeHash}" >
    <td>${employeeSkills.key.id}</td>
</c:forEach>

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

Answers


Sheetal Mohan Sharma February 2016

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

<table>
  <TH>Key</th>
  <TH>Value</th>
  <c:forEach items="${employeeObjMap }" var="current">
    <tr>
      <td><c:out value="${current.key}" /><td>
      <td><c:out value="${current.value}" /><td>
    </tr>
  </c:forEach>
</table>


Emidio Dias February 2016

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

<c:forEach var="employeeHash" items="${employeeHash}" >
    <td>${employeeSkills.key.id}</td>
    <td>${employeeSkills.value}</td>
</c:forEach>

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}" >
    <tr>
      <td>${entry.key}</td>
      <td>${skillId}</td>
    </tr>
  </c:forEach>
</c:forEach>

Post Status

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

Search




Leave an answer