Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Java in General and the fly likes HashMap problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap problem" Watch "HashMap problem" New topic
Author

HashMap problem

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 185

Dear friends,

I have created a HashMap with no type specifications.
HashMap hashmapObj = new HashMap();

But i want to put values with more than one type.
For example.

hashmapObj.put("key1",StringValue);
hashmapObj.put("key2",ArrayListValue);
hashmapObj.put("key3",InetgerValue); etc.,

When i do like this, compiler produces a warning regarding type safety.
How to handle this without any warnings. I have a condition that i must return only one HashMap object.

Kindly provide your help.
Regards,
Vijay
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14668
    
  25

Just create the map like this:

Map<String, Object> map = new HashMap<String, Object>();


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 185

Thank you very much Jesper
 
Have you tried LearnNowOnline? http://www.learnnowonline.com/
 
subject: HashMap problem