patelajk
asked on
Using HashMaps, can't get 'put' method to work.
I get the error 'Cannot resolve symbol' (the symbol being the '.' after the name of the HashMap) when using the put method when trying to add entries to a HashMap. I'm using Java 2 SDK1.4 testing my scripts currently on Windows.
I've tried finding out about this error using web-searches and various Java books but I guess it must be something to do with the Java setup I'm using. Any help would be most appreciated.
Here is the code
import java.util.HashMap;
public class HashDemoGeneric {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put(1, "Ian");
map.put(42, "Scott");
map.put(123, "Somebody else");
String name = map.get(42);
System.out.println(name);
}
}
I've tried finding out about this error using web-searches and various Java books but I guess it must be something to do with the Java setup I'm using. Any help would be most appreciated.
Here is the code
import java.util.HashMap;
public class HashDemoGeneric {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put(1, "Ian");
map.put(42, "Scott");
map.put(123, "Somebody else");
String name = map.get(42);
System.out.println(name);
}
}
and of course:
>> String name = map.get(42);
should be
String name = map.get(new Integer(42));
>> String name = map.get(42);
should be
String name = map.get(new Integer(42));
Another correction:
Because get() returns just an Object, you should cast the result to a String:
String name = (String) map.get(new Integer(42));
Because get() returns just an Object, you should cast the result to a String:
String name = (String) map.get(new Integer(42));
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you update to the latest version of the JDK, you can use your code as is
>>you can use your code as is
Well, not *quite* as is, but if you upgrade, let me know
Well, not *quite* as is, but if you upgrade, let me know
The following prints 'Scott'
import java.util.HashMap;
public class HashDemoGeneric {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "Ian");
map.put(42, "Scott");
map.put(123, "Somebody else");
String name = map.get(42);
System.out.println(name);
}
}
import java.util.HashMap;
public class HashDemoGeneric {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "Ian");
map.put(42, "Scott");
map.put(123, "Somebody else");
String name = map.get(42);
System.out.println(name);
}
}
Thank you
ASKER
Thanks for the help with this one! IT support in this place is non-existent as they are really understaffed. Haven't managed to update to the latest JDK yet.
>> map.put(1, "Ian");
>> map.put(42, "Scott");
>> map.put(123, "Somebody else");
by
map.put(new Integer(1), "Ian");
map.put(new Integer(42), "Scott");
map.put(new Integer(123), "Somebody else");
Keys and values should be objects