0

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

Joe
  • 2,543
  • 3
  • 25
  • 49
  • Sorry but I do not understand the question. Hashmaps are not intended to accept duplicate keys so it is indeed a good start to convert your `HashMap` into a `HashMap>`. But I do not understand where is the blocking point. – Arnaud Denoyelle Feb 08 '16 at 13:38
  • the blocking point is how im iterating on hashmap in a jsp file and printing it to the html. @ArnaudDenoyelle – Joe Feb 08 '16 at 13:40
  • Does this link help you ? http://stackoverflow.com/questions/1835683/how-to-loop-through-a-hashmap-in-jsp You actually need to nest 2 `c:forEach` : one on `map.entrySet` and the other on `entry.getValue` (which is a list) – Arnaud Denoyelle Feb 08 '16 at 13:44
  • Probably same question: [here](http://stackoverflow.com/questions/1835683/how-to-loop-through-a-hashmap-in-jsp), please check – Akceptor Feb 08 '16 at 13:44
  • Another hint : Do you really need a `HashMap`? Sometimes, creating an intermediate class makes your code clearer. Creating a simple `Pair` class enables you to handle a simple `List`. – Arnaud Denoyelle Feb 08 '16 at 14:14

3 Answers3

0

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>
Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24
0

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

Community
  • 1
  • 1
  • While this may answer the question, [it would be preferable](http://meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Panda Feb 08 '16 at 13:50
  • do i need to send my hashmap to the session using request.getSession.setAttribute(myHashMap) and then I will be able to access it? cause now i can't – Joe Feb 08 '16 at 13:58
0

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>
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148