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.

+3


source to share


3 answers


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]}"/>

      

+1


source


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?

0


source


I found a workaround.

    <fmt:parseNumber var="prevLineNumberKey" type="number" value="${lineNumber-1}" />
    <c:out value="Previous ${hashtableObj[sortedList[prevLineNumberKey-1]]}" escapeXml="false"/><br/>

      

I used list item as key for Hashtable and it works. Thanks everyone.

0


source







All Articles