从没有索引值的HashMap访问值?

时间:2012-04-25 11:19:30

标签: java jsp

我的hashmap中有两个值...如何在不使用for循环的情况下访问这些值。

这是我的hashmap代码:

HashMap<Integer, Integer> obMap = new HashMap<Integer, Integer>();
obMap.put(new Integer(1),PartnerID);
obMap.put(new Integer(2),numwidgets);
return obMap;

我正在将值返回给java脚本方法...如果我进入obMap,如何读取JSP中的值。

4 个答案:

答案 0 :(得分:3)

HashMap<Integer, Integer> obMap = new HashMap<Integer, Integer>();
obMap.put(1,PartnerID);
obMap.put(2,numwidgets);
System.out.println(obMap.get(1));
System.out.println(obMap.get(2));

请注意,您不需要new Integer(1)。这是自动装箱。

答案 1 :(得分:0)

你应该可以直接从java脚本

调用obMap.get(key)

答案 2 :(得分:0)

如果您在JSP中工作,并且在jsp中有objMap HashMap,那么您可以在javascript中使用此scriplet访问它:

<%=obMap.get(1)%>

答案 3 :(得分:0)

在JSP页面中,您可以通过以下方式访问HashMap

<%=obMap.get(0)%> <!-- The first value in the map -->
<%=obMap.get(1)%> <!-- The second value in the map -->

这可以放在JSP页面的JavaScript部分的JavaScript函数或块中 请注意,JSP页面中的Java scriplets以&lt;%和%&gt;开头和结尾。分别
当您想要将值直接包含在JavaScript变量&lt;%=和%&gt;中时用于开始和结束上面的值。

希望这个答案可以帮到你。

相关问题