Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > HashMap<Object, Double> returns what instead of null?

Reply
Thread Tools

HashMap<Object, Double> returns what instead of null?

 
 
aaronfude@gmail.com
Guest
Posts: n/a
 
      05-09-2006
Hi,

What is get(Object) supposed to return when called on an object of type
HashMap<Object, Double> as in:

double x = hashMap.get(key);

when the key is not available?

Thanks!

Aaron Fude

 
Reply With Quote
 
 
 
 
Rhino
Guest
Posts: n/a
 
      05-09-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi,
>
> What is get(Object) supposed to return when called on an object of type
> HashMap<Object, Double> as in:
>
> double x = hashMap.get(key);
>
> when the key is not available?
>

Without actually trying it, I'm guessing that you'd get a null if you did a
get() on a key that didn't exist. But why guess? If I were you, I'd just
write a few lines of code then try it in your debugger and see what actually
happens. An actual experiment is always better than an educated guess if the
experiment isn't too hard to conduct.

--
Rhino


 
Reply With Quote
 
 
 
 
Simon
Guest
Posts: n/a
 
      05-09-2006
Rhino wrote:
>>What is get(Object) supposed to return when called on an object of type
>>HashMap<Object, Double> as in:
>>
>>double x = hashMap.get(key);
>>
>>when the key is not available?
>>

>
> Without actually trying it, I'm guessing that you'd get a null if you did a
> get() on a key that didn't exist.


The problem is that a double cannot be null. Think of the above line of code as

double x = hashMap.get(key).doubleValue();

This will throw a NPE as will the original code. The same happens for

Double d = null;
double x = d;

Cheers,
Simon
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      05-09-2006
Simon wrote:
>
> The problem is that a double cannot be null. Think of the above line of code as
>
> double x = hashMap.get(key).doubleValue();
>
> This will throw a NPE as will the original code. The same happens for
>
> Double d = null;
> double x = d;


The outrageous dangers of auto*un*boxing...

If you do want a "null" for double, don't use a very small number.
Double.NaN makes a suitable substitute. Note, to check for NaNs use
Double.isNan. IEEE 754 demands Double.NaN != Double.NaN...

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      05-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> What is get(Object) supposed to return when called on an object of type
> HashMap<Object, Double> as in:
>
> double x = hashMap.get(key);
>
> when the key is not available?


Exactly the same as if you tried to unbox null. I.e. the same as if you'd
executed:

Double d = null;
double x = d;

or, the exact equivalent to the above two lines:

Double d = null;
double x = d.doubleValue();

Why should you expect that to do anything except throw a NullPointerException ?

-- chris



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorted Returns List and Reversed Returns Iterator ++imanshu Python 7 08-23-2008 04:25 AM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
block returns and hash element returns Trans Ruby 2 11-06-2005 12:15 PM
dr("field").toString returns "400.0000" instead of "400" Dan C Douglas ASP .Net 5 07-22-2003 05:48 PM



Advertisments