5

I am storing a double value inside the a HashMap as shown

HashMap listMap = new HashMap();

double mvalue =0.0;

listMap.put("mvalue",mvalue );

Now when i tried to retrieve that value , as shown

mvalue = Double.parseDouble((String) listMap.get("mvalue"));

i am getting an error as

java.lang.Double cannot be cast to java.lang.String

I am confused here ,

This is my actual HashMap and i am setting the values in it as shown

HashMap listMap = new HashMap();
double mvalue =0.0 ;
List<Bag> bagList = null; 
listMap.put("bagItems",bagList);
listMap.put("mvalue", mvalue);

Could anybody please tell me , how the structure of the HashMap should be ?

1

8 Answers 8

8

You have put a Double in the Map. Don't cast to String first. This will work:

HashMap<String, Double> listMap = new HashMap<String, Double>();
mvalue = listMap.get("mvalue");

Your primitive double is being Autoboxed to a Double Object. Use Generics to avoid the need to cast, which is the <String, Double> part.

Sign up to request clarification or add additional context in comments.

Comments

3

I guess your map will store many diferents types So I recomend <String, Object> generic

HashMap listMap = new HashMap<String, Object>();
double mvalue =0.0;
listMap.put("mvalue",mvalue );
.
.
.
String mValueString = Double.toString((Double) listMap.get("mvalue"));

This will get you the double object, cast it to Double, and the convert into string in new variable.

1 Comment

Thanks. HashMap listMap = new HashMap<String, Object>(); really solved the problem.
1

how about

mvalue = listMap.get("mvalue");

?

Comments

1

First of all, make a HashMap with Class Types defined , like : HashMap<String,Double>

Secondly , this should work :

mvalue = Double.parseDouble(Double.toString(listMap.get("mvalue")));

Although, best way is :

mvalue = listMap.get("mvalue");
// provided defined HashMap is HashMap<String,Double>

4 Comments

if map stores double value then why to parse it? Here you are doing 2 unnecessary parsing. first double to string and then again string to double.
Sure, it works, but why go through the trouble of converting back and forth between String and Double when the value is already a Double?
yeah i knw..And i modified my answer to suit its needs..but i wanted to tell @yyy i 777 , how to go about in this situation..!!
The Map already holds a Double. If mvalue needs to be a primitive then unboxing will do it. Why on earth would you do the toString call, only to parse it as a Double ?
1

If you introduce generics you will immeditely discover the probblem

Map<String, Double> listMap = new HashMap<String, Double>();
Double mvalue = listMap.get("mvalue");

The reason is that your map does return a Double and not a String, hence the error message.

Comments

0

try this :

HashMap<String, Double> listMap = new HashMap<String, Double>();
Double mvalue = 0.0;
listMap.put("mvalue", mvalue);
mvalue = listMap.get("mvalue");

Comments

0

Your code should looks like:

Map<String,Double> listMap = new HashMap<String,Double>();
double mvalue = 0.0;
listMap.put("mvalue", mvalue );
mvalue = Double.parseDouble( String.valueOf(listMap.get("mvalue")) );

Notice that if you need to retrieve a wrapper and not a primitive type, you can use on wrapper classes XXX.valueOf() instead of XXX.parseXXX() which returns a primitive.

Comments

0
mvalue = Double.parseDouble(String.valueOf(listMap.get("mvalue")));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.