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

A

aaronfude

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
 
R

Rhino

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.
 
S

Simon

Rhino said:
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
 
T

Thomas Hawtin

Simon said:
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
 
C

Chris Uppal

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top