get string value from HashMap depending on key name
Solution 1
Just use Map#get(key)
?
Object value = map.get(myCode);
Here's a tutorial about maps, you may find it useful: https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html.
Edit: you edited your question with the following:
I'm expecting to see a String, such as "ABC" or "DEF" as that is what I put in there initially, but if I do a System.out.println() I get something like java.lang.string#F0454
Sorry, I'm not too familiar with maps as you can probably guess ;)
You're seeing the outcome of Object#toString()
. But the java.lang.String
should already have one implemented, unless you created a custom implementation with a lowercase s
in the name: java.lang.string
. If it is actually a custom object, then you need to override Object#toString()
to get a "human readable string" whenever you do a System.out.println()
or toString()
on the desired object. For example:
@Override
public String toString() {
return "This is Object X with a property value " + value;
}
Solution 2
If you are storing keys/values as strings, then this will work:
HashMap<String, String> newMap = new HashMap<String, String>();
newMap.put("my_code", "shhh_secret");
String value = newMap.get("my_code");
The question is what gets populated in the HashMap (key & value)
Solution 3
If you will use Generics and define your map as
Map<String,String> map = new HashMap<String,String>();
then fetching value as
String s = map.get("keyStr");
you wont be required to typecast the map.get() or call toString method to get String value
Solution 4
Your question isn't at all clear I'm afraid. A key doesn't have a "name"; it's not "called" anything as far as the map is concerned - it's just a key, and will be compared with other keys. If you have lots of different kinds of keys, I strongly suggest you put them in different maps for the sake of sanity.
If this doesn't help, please clarify the question - preferrably with some code to show what you mean.
Solution 5
map.get(myCode)
Comments
-
Jimmy over 2 years
I have a
HashMap
with various keys and values, how can I get one value out?I have a key in the map called
my_code
, it should contain a string, how can I just get that without having to iterate through the map?So far I've got..
HashMap newMap = new HashMap(paramMap); String s = newMap.get("my_code").toString();
I'm expecting to see a
String
, such as "ABC" or "DEF" as that is what I put in there initially, but if I do aSystem.out.println()
I get something likejava.lang.string#F0454
Sorry, I'm not too familiar with maps as you can probably guess ;)
-
Tyler about 14 yearsOf course, if you are creating a custom string class, I would recommend (1) not doing that, since it's probably not what you want to do, and (2) if you are going to do that, DO NOT put it in the java.lang namespace.
-
saidesh kilaru over 11 yearsif HashMap<String, String> newMap = new HashMap<String, String>(); how to get string element from this...? where String value = newMap.get("my_code"); gives error
-
saidesh kilaru over 11 yearsif HashMap<String, String> newMap = new HashMap<String, String>(); how to get string element from this...? where String value = newMap.get("my_code"); gives error
-
Admin over 8 yearshow you can inverse ? get key from value ?
-
Andy almost 3 yearsdon't the two
"my_code"
strings have different references? why does this work?