Hi Rick,

I think the fundamental problem is that a Java int is
a primitve, and an Integer is an Object.  The 32 bit
int is a 2's compliment representation of a number. 
The 32 bit Integer is a reference to the location in
memory where that Integer Object resides.  Not the
same thing at all.  

The java.util.Map only takes Objects as keys and
values.  If you've got an int that you want to use as
the key to get a value out of your Map, you'll have to
wrap it as a java.lang.Integer first.

I thought about something like this:

<%@ page language="java" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.TreeMap" %>

<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core"; %>

<%

    Map users = new TreeMap();
    
    users.put(new Integer(1), "foo");
    users.put(new Integer(2), "bar");
    users.put(new Integer(3), "baz");
    
    Integer key = new Integer(3);

%>

<c:out value="Here's the value you ordered:
${users.get[key]}"/>

But it's not working.  Unless the EL can handle
passing an Object to a Map, I don't know a way to do
what you're asking.  The int-to-String is the only
workaround that I'm aware of. - MOD


--- Rick Reumann <[EMAIL PROTECTED]> wrote:
> Yesterday N. Chen was kind in helping me with
> question I had concerning
> pulling a bean from a Map where the key is an
> Integer. Sorry, I don't
> have the original post here at work so I have to
> start a new thread.
> 
> The solution provided works great if the key in the
> map is a String, but
> I still can't get it to work when the key is an
> Integer.
> 
> For example trying to do the following will work
> fine if the Map uses a
> String "3" for the key, but doesn't work when 3 as
> an Integer is used
> for the key...
> 
> <c:set var="laborReport"
> value='${mapOfLaborReports["3"]}' />
> 
> <c:out value="${laborReport.conceptId}"/>
> 
> 
> I also have a request variable in scope called
> "currentLaborReportLevel"
> which has as its value an Integer. Ideally I'd like
> to be able to do as
> N. Chen helped me with...
> 
> <c:set var="laborReport"
>
value='${mapOfLaborReports[currentLaborReportLevel]}'
> /> 
> 
> But this also only seems to work when
> currentLaborReportLevel is a
> String and the mapOfLaborReports keys are stored as
> Strings.
> 
> I understand that there are other ways I can
> accomplish what I need to
> do without using the Map on the page, and I'm
> leaning towards doing it
> that way, but I would be interested to know if this
> could be done.
> 
> Thanks.
> 
> The reply and original message which I have a copy
> of is below...
> 
> 
> 
> 
> Date: Wed, 16 Jul 2003 16:50:05 -0700 (PDT)
> From: "N. Chen" <[EMAIL PROTECTED]>
> To: Tag Libraries Users List
> <[EMAIL PROTECTED]>
> Subject: Re: How to retrieve a value from a Map
> where key is an Integer
> & ..?
> 
> 
> 
> I believe you can do (with reportID a variable
> containing the key)
> <:set var="laborReport"
> value='${mapOfLaborReports[reportID]}'/>
> 
> which is very different from (with "reportID" as the
> key)
> <:set var="laborReport"
> value='${mapOfLaborReports["reportID"]}'/>
> 
> so to use 3 as a key
> <c:set var="laborReport"
> value='${mapOfLaborReports["3"]}'/>
> 
> nick
> On 16 Jul 2003, Rick Reumann wrote:
> 
> > There is probably a real easy answer for these two
> questions, but I
> > searching the archives for a while hasn't
> helped...
> >
> > 1) I have beans in a map that are stored with
> integers for the keys.
> >
> > For testing I can retrive the bean out fine with
> the key of "test"
> > like:
> >
> > <c:set var="laborReport"
> value="${mapOfLaborReports.test}"/>
> >
> > But if I try this with...
> >
> > <c:set var="laborReport"
> value="${mapOfLaborReports.3}"/>
> >
> > I get a parsing error:
> >
> > 4: tag = 'set' / attribute = 'value': An error
> occurred while parsing
> > custom action attribute "value" with value
> "${mapOfLaborReports.3}":
> > Encountered ".3", expected one of ["}", ".", ">",
> "gt", "<", "lt",
> > "==","eq", "<=", "le", ">=", "ge", "!=", "ne",
> "[", "+", "-", "*",
> > "/","div", "%", "mod", "and", "&&", "or", "||",
> ":", "("]
> >
> >
> >
> > 2) Also, is there a way that I can some how nest
> my expressions? In
> > other words say "test" is the value of a parameter
> with the name
> > "reportID" in scope. Now in the first example I'd
> like to be able to
> > do something like...
> >
> > <:set var="laborReport"
> value="${mapOfLaborReports.${reportID}}"/>
> >
> > (Tied into the first question reportID is an
> Integer but if need be I
> > can make sure it's a String representation of the
> Integer).
> >
> >
> > Thanks for any help.
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> For additional commands, e-mail:
> [EMAIL PROTECTED]
> 


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to