Hashtable key manipulation not working
I am trying to access a Hashtables value based on its key, which is a number as a String in JSTL. But if I increase / decrease the key value it doesn't work anymore.
I am iterating over a sorted list of keys in a for loop. I am using this element to access the Hashtable.
<c:forEach items="${helper:getSortedList(hashtableObj)}" var="lineNumber" varStatus="loop">
<c:if test="${param.lineNbr eq lineNumber}">
<c:if test="${lineNumber>1}">
<fmt:parseNumber var="prevLineNumberKey" type="number" value="${lineNumber-1}" />
<c:out value="PREV ${hashtableObj[prevLineNumberKey]}" escapeXml="false"/><br/>
</c:if>
<c:out value="Current :${lineNumber}" /><br/>
<c:if test="${lineNumber<fn:length(hashtableObj)-1}">
<fmt:parseNumber var="nextLineNumberKey" type="number" value="${lineNumber+1}" />
<c:out value="NEXT ${hashtableObj[nextLineNumberKey+1]}" escapeXml="false"/><br/>
</c:if>
</c:if>
</c:forEach>
Output:
PREV
Current: 51
NEXT
But I expected
PREV 50
Current: 51
NEXT 52
Any pointers are appreciated.
source to share
If the keys are in Map
String
than to get the item, you must query it using a value String
. Your current solution is asking for a value Map
from Long
.
You can convert the number to String
and then query Map
like this:
<c:set var="numberAsString">${50 - 1}</c:set>
<c:out value="value: ${hashtableObj[numberAsString]}"/>
source to share
Try replacing:
<fmt:parseNumber var="prevLineNumberKey" type="number" value="${lineNumber-1}" />
FROM
<c:set var="prevLineNumberKey">${lineNumber-1}</c:set>
And replace:
<fmt:parseNumber var="nextLineNumberKey" type="number" value="${lineNumber+1}" />
<c:out value="NEXT ${hashtableObj[nextLineNumberKey+1]}" escapeXml="false"/><br/>
FROM
<c:set var="nextLineNumberKey">${lineNumber+1}</c:set>
<c:out value="NEXT ${hashtableObj[nextLineNumberKey]}" escapeXml="false"/><br/>
A couple of questions:
1) Is hashtableObj really a hash table or is it a hash map? 2) Is the hashtableObj value really a number that is equal to the key? In other words, you expect:
PREV 50
... does this mean you expect the hashtable / map value to be 50 and the key is also 50?
source to share