0

I have a HashMap which looks like the following:

HashMap<String, Integer> jcbs = new HashMap<String, Integer>();

The key is a String, the Value an Integer.

Now I have a html-file in which there is a table.

What I need to do now, is to fill all the keys and values of the HashMap into this html table.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Stefan
  • 49
  • 1
  • 1
  • 3
  • 1
    can u give more details? where is the HashMap client side server side? what programming language. – bubakazouba Apr 28 '15 at 06:11
  • why have you selected key as string? – Danyal Sandeelo Apr 28 '15 at 06:11
  • My HashMap is client side - the programming language is java – Stefan Apr 28 '15 at 06:22
  • And the point, why i've selected a String as key, is because my second field is a counter, and so the key cant be an Integer, because there could be the same counter twice as key, and that would lead to an error or something i think – Stefan Apr 28 '15 at 06:23

4 Answers4

1
<%

    StringBuilder stringMapTable = new StringBuilder();
    stringMapTable.append("<table>");

    Iterator it = jcbc.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry)it.next();
            stringMapTable.append("<tr><td>" + entry.getKey() + "</td><td>" +entry.getValue() + "</td></tr>");
            System.out.println(pair.getKey() + " = " + pair.getValue());
            it.remove(); // avoids a ConcurrentModificationException
        }

  String mapTable = stringMapTable.toString();

%>

In HTML

<%=mapTable %>
Kurenai Kunai
  • 1,842
  • 2
  • 12
  • 22
1

To iterate and display the hashmap in a html page in either jsp or jsf can be done in the following ways:

In case of JSP , use jstl :

Map books = new HashMap();

<c:forEach var="booksVar" items="${books}">
    Book Id: ${books.key} , Capital: ${books.value}
</c:forEach>

This can be easily used with Native html table code.

Ashoka
  • 935
  • 7
  • 20
1

With advent of java8 , we can create the html table in pure java by using stream api .

    final Map<String,Integer> map =  new HashMap<>();
    
    map.put("key1", 5);
    map.put("key2", 7);
    
    String tableBody =
    map.keySet()
       .stream()
       .map(item -> new StringBuilder("<tr><td>")
                                     .append(item)
                                     .append("</td><td>")
                                     .append(String.valueOf(map.get(item)))
                                     .append("</td></tr>")
       )
       .collect(Collectors.joining());
    
    String tableContent = "<table>" + tableBody + "</table>";
    
    System.out.println("tableContent = "+tableContent);

To create a table in jsp from a map , better opt for JSTL.

    <table>
        <c:forEach var="entry" items="${mapObj}">
            <tr><td>${entry.key}</td> <td><${entry.value}</td></tr>
        </c:forEach>
    </table>
Vishy Anand
  • 93
  • 1
  • 10
0

Could be as simple as this:

StringBuilder htmlBuilder = new StringBuilder();
htmlBuilder.append("<table>");

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    htmlBuilder.append(String.format("<tr><td>%s</td><td>%d</td></tr>",
            entry.getKey(), entry.getValue()));
}

htmlBuilder.append("</table>");

String html = htmlBuilder.toString();
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • I've implemented this into my programm, but is isnt working. My question is: where do I have to specify the directory, where this html has to be written to? – Stefan Apr 28 '15 at 06:22
  • Are you just looking to write output to a file? I'm sure Google can help you with that. – Robby Cornelissen Apr 28 '15 at 06:23
  • Yes thats what i want to do: I have my HashMap, already filled with data, and I just have to generate an HTML with a table inside it, and fill all the keys and values into this table – Stefan Apr 28 '15 at 06:25
  • I gave you the code to generate the HTML. The rest you can find e.g. here: http://stackoverflow.com/questions/2885173/java-how-to-create-a-file-and-write-to-a-file – Robby Cornelissen Apr 28 '15 at 06:27