Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 237393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:25:18+00:00 2026-05-11T20:25:18+00:00

I have a Map keyed by Integer. Using EL, how can I access a

  • 0

I have a Map keyed by Integer. Using EL, how can I access a value by its key?

Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");

I thought this would work but it doesn’t (where map is already in the request’s attributes):

<c:out value="${map[1]}"/>

Follow up: I tracked down the problem. Apparently ${name[1]} does a map lookup with the number as a Long. I figured this out when I changed HashMap to TreeMap and received the error:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

If I change my map to be:

Map<Long, String> map = new HashMap<Long, String>();
map.put(1L, "One");

then ${name[1]} returns “One”. What’s with that? Why does <c:out> treat a number as a long. Seems counterintuitive to me (as int is more commonly used than long).

So my new question is, is there a EL notation to access a map by an Integer value?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:25:18+00:00Added an answer on May 11, 2026 at 8:25 pm

    Initial answer (EL 2.1, May 2009)

    As mentioned in this java forum thread:

    Basically autoboxing puts an Integer object into the Map.
    ie:

    map.put(new Integer(0), "myValue")
    

    EL (Expressions Languages) evaluates 0 as a Long and thus goes looking for a Long as the key in the map.
    ie it evaluates:

    map.get(new Long(0))
    

    As a Long is never equal to an Integer object, it does not find the entry in the map.
    That’s it in a nutshell.


    Update since May 2009 (EL 2.2)

    Dec 2009 saw the introduction of EL 2.2 with JSP 2.2 / Java EE 6, with a few differences compared to EL 2.1.
    It seems (“EL Expression parsing integer as long“) that:

    you can call the method intValue on the Long object self inside EL 2.2:

    <c:out value="${map[(1).intValue()]}"/>
    

    That could be a good workaround here (also mentioned below in Tobias Liefke‘s answer)


    Original answer:

    EL uses the following wrappers:

    Terms                  Description               Type
    null                   null value.               -
    123                    int value.                java.lang.Long
    123.00                 real value.               java.lang.Double
    "string" ou 'string'   string.                   java.lang.String
    true or false          boolean.                  java.lang.Boolean
    

    JSP page demonstrating this:

     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    
     <%@ page import="java.util.*" %>
    
     <h2> Server Info</h2>
    Server info = <%= application.getServerInfo() %> <br>
    Servlet engine version = <%=  application.getMajorVersion() %>.<%= application.getMinorVersion() %><br>
    Java version = <%= System.getProperty("java.vm.version") %><br>
    <%
      Map map = new LinkedHashMap();
      map.put("2", "String(2)");
      map.put(new Integer(2), "Integer(2)");
      map.put(new Long(2), "Long(2)");
      map.put(42, "AutoBoxedNumber");
    
      pageContext.setAttribute("myMap", map);  
      Integer lifeInteger = new Integer(42);
      Long lifeLong = new Long(42);  
    %>
      <h3>Looking up map in JSTL - integer vs long </h3>
    
      This page demonstrates how JSTL maps interact with different types used for keys in a map.
      Specifically the issue relates to autoboxing by java using map.put(1, "MyValue") and attempting to display it as ${myMap[1]}
      The map "myMap" consists of four entries with different keys: A String, an Integer, a Long and an entry put there by AutoBoxing Java 5 feature.       
    
      <table border="1">
        <tr><th>Key</th><th>value</th><th>Key Class</th></tr>
        <c:forEach var="entry" items="${myMap}" varStatus="status">
        <tr>      
          <td>${entry.key}</td>
          <td>${entry.value}</td>
          <td>${entry.key.class}</td>
        </tr>
        </c:forEach>
    </table>
    
        <h4> Accessing the map</h4>    
        Evaluating: ${"${myMap['2']}"} = <c:out value="${myMap['2']}"/><br>
        Evaluating: ${"${myMap[2]}"}   = <c:out value="${myMap[2]}"/><br>    
        Evaluating: ${"${myMap[42]}"}   = <c:out value="${myMap[42]}"/><br>    
    
        <p>
        As you can see, the EL Expression for the literal number retrieves the value against the java.lang.Long entry in the map.
        Attempting to access the entry created by autoboxing fails because a Long is never equal to an Integer
        <p>
    
        lifeInteger = <%= lifeInteger %><br/>
        lifeLong = <%= lifeLong %><br/>
        lifeInteger.equals(lifeLong) : <%= lifeInteger.equals(lifeLong) %> <br>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 221k
  • Answers 221k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want more than a copypaste of the single… May 13, 2026 at 12:16 am
  • Editorial Team
    Editorial Team added an answer That's real life : you need to account for this… May 13, 2026 at 12:16 am
  • Editorial Team
    Editorial Team added an answer If it's not enough to use the -XX:HeapDumpOnOutOfMemoryError command line… May 13, 2026 at 12:16 am

Related Questions

I want to allow the user to define a query which will be used
I'm considering using CouchDB for an upcoming site, but I'm a little confused as
Please excuse my ignorance on the topic, as I am relatively new to Hibernate
I have a Map which is to be modified by several threads concurrently. There

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.