So I have two questions about HashMap
s in Java:
What is the correct way to initialize a
HashMap
? I think it might be best in my situation to use:HashMap x = new HashMap();
But Eclipse keeps suggesting that I use:
HashMap<something, something> map = new HashMap();
Which is better?
Can a
HashMap
hold different types of objects/data types as values? For example, would this work and be OK:map.put("one", 1); map.put("two", {1, 2}); map.put("three", "hello");
In the first
put()
, I want anint
as a value, in the second anint[]
, and third a string. Is this okay to do in Java withHashMap
s? Also, is it okay to store aHashMap
as a value within aHashMap
?