[prev in list] [next in list] [prev in thread] [next in thread] 

List:       taglibs-user
Subject:    Re: ClassCastException when changing from HashMap to TreeMap
From:       "Kris Schneider" <kris () directthought ! com>
Date:       2008-10-17 16:51:31
Message-ID: b9943af40810170951yf1a7bbbyabde0db860674d3 () mail ! gmail ! com
[Download RAW message or body]

Just ran a couple quick tests, and it looks like integer literals are
converted to Long. I completely blocked on the fact that HashMap will
also attempt to do aLong.equals(anInteger), which will fail.

On Fri, Oct 17, 2008 at 12:20 PM, Kris Schneider <kris@directthought.com> wrote:
> HashMap will essentially just use the hashCode value of the key to do
> the lookup. So, when using "3", the hashCode value is 51. But for both
> Integer(3) and Long(3), the hashCode value is 3. If the conversion is
> actually to BigDecimal, then the hashCode value of BigDecimal(3) is 93
> and that might explain the miss even when using a numeric literal.
> 
> On Fri, Oct 17, 2008 at 12:04 PM, Gabriel Belingueres
> <belingueres@yahoo.com.ar> wrote:
> > 
> > I now I realize that it is returning nothing on ${map[param.myid]} with
> > HashMap. But surprisingly even though I know for sure that map[3] exists,
> > then ${map[3]} (with the literal 3) returns nothing too...I remember
> > previously having an issue with this because IIRC JSTL converts the 3 to a
> > Long, not to Integers.
> > 
> > 
> > Kris Schneider wrote:
> > > 
> > > It looks like it might be a difference in the way HashMap.containsKey
> > > and TreeMap.containsKey are implemented. If you look at the API docs,
> > > TreeMap will throw ClassCastException but HashMap will not. When you
> > > were using HashMap, did you actually get values returned or null?
> > > 
> > > On Fri, Oct 17, 2008 at 10:48 AM, Gabriel Belingueres
> > > <belingueres@yahoo.com.ar> wrote:
> > > > 
> > > > Hi,
> > > > 
> > > > I was accessing a Map<Integer, MyClass> implemented with a
> > > > HashMap<Integer,
> > > > MyClass> from JSTL (v.1.1.2) using the following expression:
> > > > 
> > > > ${map[param.myid].name} and it worked well.
> > > > 
> > > > However, when I changed the implementation to a TreeMap<Integer, MyClass>
> > > > the expression stop working because it throws ClassCastException saying
> > > > that
> > > > can't compare Strings with Integers.
> > > > 
> > > > I know that ${param.myid} should resolve to a String, but I wanted to
> > > > clarify if this is a normal behaviour or it is a bug, since it seems to
> > > > be
> > > > converting to Integer for HashMap but not for TreeMap.
> > > > 
> > > > Best regards,
> > > > Gabriel
> > > > 
> > > > --
> > > > View this message in context:
> > > > http://www.nabble.com/ClassCastException-when-changing-from-HashMap-to-TreeMap-tp20034818p20034818.html
> > > >  Sent from the Taglibs - User mailing list archive at Nabble.com.
> > > > 
> > > > 
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> > > > For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
> > > > 
> > > > 
> > > 
> > > 
> > > 
> > > --
> > > Kris Schneider <mailto:kris@directThought.com>
> > > directThought  <http://www.directThought.com/>
> > > 
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
> > > 
> > > 
> > > 
> > 
> > --
> > View this message in context: \
> > http://www.nabble.com/ClassCastException-when-changing-from-HashMap-to-TreeMap-tp20034818p20036227.html
> >  Sent from the Taglibs - User mailing list archive at Nabble.com.
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
> > 
> > 
> 
> 
> 
> --
> Kris Schneider <mailto:kris@directThought.com>
> directThought  <http://www.directThought.com/>
> 



-- 
Kris Schneider <mailto:kris@directThought.com>
directThought  <http://www.directThought.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic