EL get value of a HashMap by Integer key

前端 未结 2 473
感情败类
感情败类 2020-12-18 08:11

I have this HashMap:

    Map odometerMap = new LinkedHashMap();
    odometerMap.put(0, getLocaleForKey(\"drop-d         


        
相关标签:
2条回答
  • 2020-12-18 08:27

    In EL, numbers are treated as Long. It's looking for a Long key. It'll work if you use Long instead of Integer as map key.

    Map<Long, String> odometerMap = new LinkedHashMap<Long, String>();
    odometerMap.put(0L, getLocaleForKey("drop-down.any"));
    odometerMap.put(1L, "< 1000");
    // ...
    
    0 讨论(0)
  • 2020-12-18 08:41

    An alternative could be using a String as the key

    Map<String, String> odometerMap;
    

    .. and:

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

    But, it's better to use a List of Strings since your key doesn't have any clear meaning:

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

    .. and:

    <c:out value="${odometers[2]}"/>
    
    0 讨论(0)
提交回复
热议问题