I want to do the below thing in JSP starting from for loop– I just want to loop HashSet and HashMap and print the result
private static HashMap<Long, Long> histogram = new HashMap<Long, Long>();
private static Set<Long> keys = histogram.keySet();
for (Long key : keys) {
Long value = histogram.get(key);
System.out.println("MEASUREMENT, HG data, " + key + ":" + value);
}
I am working with Spring MVC, so I added these two things in my model
model.addAttribute("hashSet", (keys));
model.addAttribute("histogram", (histogram));
And in my JSP page, I was doing something like this to emulate the above JAVA code but it was giving me an exception that something is wrong in my JSP page.
<fieldset>
<legend>Performance Testing:</legend>
<pre>
<c:forEach items="${hashSet}" var="entry">
Key = ${entry.key}, value = ${histogram}.get(${entry.key})<br>
</c:forEach>
</pre>
<br />
</fieldset>
Exception I got-
Caused by: javax.el.PropertyNotFoundException: Property 'key' not found on type java.lang.Long
at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:195)
at javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:172)
at javax.el.BeanELResolver.property(BeanELResolver.java:281)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:62)
Can anyone help me out with this?
You don’t need to make use of
keySetto access thevaluesin theHashMap. When you iterate overHashMapusing<c:forEach..>, you get back theEntrySet, for which you can use: –EntrySet#getKey()andEntrySet#getValue()directly: –