EL Gets HashMap with Integer Key

I have this HashMap:

    Map<Integer, String> odometerMap = new LinkedHashMap<Integer, String>();
    odometerMap.put(0, getLocaleForKey("drop-down.any"));
    odometerMap.put(1, "< 1000");
    odometerMap.put(2, "1000 - 5000");
    odometerMap.put(3, "5000 - 10000");
    odometerMap.put(4, "10000 - 20000");
    odometerMap.put(5, "20000 - 30000");
    odometerMap.put(6, "30000 - 40000");
    odometerMap.put(7, "40000 - 60000");
    odometerMap.put(8, "60000 - 80000");
    odometerMap.put(9, "> 80000");

My goal in JSP is to print, for example, $ {odometerMap [2]} (the result is an empty string):

    <c:out value="${odometerMap[2]}"/>

If I print only $ {odometerMap}, I get the full map:

{0=Any, 1=< 1000, 2=1000 - 5000, 3=5000 - 10000, 4=10000 - 20000, 5=20000 - 30000, 6=30000 - 40000, 7=40000 - 60000, 8=60000 - 80000, 9=> 80000}

How can I print only the item of my choice? Example: 2?

thank

+5
source share
2 answers

In EL, numbers are counted Long. He is looking for a key Long. It will work if you use cards as a key Longinstead Integer.

Map<Long, String> odometerMap = new LinkedHashMap<Long, String>();
odometerMap.put(0L, getLocaleForKey("drop-down.any"));
odometerMap.put(1L, "< 1000");
// ...
+11
source

An alternative could be to use Stringas a key

Map<String, String> odometerMap;

.. and:

<c:out value="${odometerMap['2']}"/>

But it is better to use Listof Strings, since your key does not have a clear meaning:

List<String> odometers = new ArrayList<String>();
odometers.add(getLocaleForKey("drop-down.any"));
// etc

.. and:

<c:out value="${odometers[2]}"/>
+5
source

All Articles