根据键名从HashMap中获取字符串值

时间:2022-01-01 20:57:55

I have a HashMap with various keys and values, how can I get one value out?

我有一个包含各种键和值的HashMap,如何从中取出一个值?

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?

我在映射my_code中有一个键,它应该包含一个字符串,我怎么才能不需要遍历映射呢?

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 a System.out.println() I get something like java.lang.string#F0454

我希望看到一个字符串,如“ABC”或“DEF”,因为这是我最初放在那里的,但是如果我使用System.out.println(),就会得到类似java.lang.string#F0454的东西

Sorry, I'm not too familiar with maps as you can probably guess ;)

对不起,我不太熟悉地图,你可能猜到了;

8 个解决方案

#1


128  

Just use Map#get(key) ?

只是利用图#(关键)?

Object value = map.get(myCode);

Here's a tutorial about maps, you may find it useful: http://java.sun.com/docs/books/tutorial/collections/interfaces/map.html.

这里有一个关于地图的教程,您可能会发现它很有用:http://java.sun.com/docs/books/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

我希望看到一个字符串,如“ABC”或“DEF”,因为这是我最初放在那里的,但是如果我使用System.out.println(),就会得到类似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:

您将看到对象#toString()的结果。但. lang。String应该已经实现了一个,除非您创建了一个名称中小写s的自定义实现:java.lang.string。如果它实际上是一个自定义对象,那么您需要重写对象#toString(),以便在需要的对象上执行System.out.println()或toString()时获得“可读的字符串”。例如:

@Override
public String toString() {
    return "This is Object X with a property value " + value;
}

#2


48  

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)

问题是在HashMap (key & value)中填充什么

#3


8  

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.

如果这没有帮助,请澄清这个问题——最好是用一些代码来说明你的意思。

#4


8  

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

您将不会被要求输入map.get()或调用toString方法来获取字符串值。

#5


5  

map.get(myCode)

map.get(myCode)

#6


4  

An important point to be noted here is that if your key is an object of user-defined class in java then make it a point to override the equals method. Because the HashMap.get(Object key) method uses the equals method for matching the key value. If you do not override the equals method then it will try to find the value simply based on the reference of the key and not the actual value of key in which case it will always return a null.

这里要注意的一个重点是,如果您的键是java中用户定义类的对象,那么请将它设置为覆盖equals方法的点。因为HashMap。get(Object key)方法使用equals方法来匹配键值。如果您不重写equals方法,那么它将尝试根据键的引用而不是键的实际值来查找值,在这种情况下,它总是返回null。

#7


1  

Suppose you declared HashMap as :-

假设您将HashMap声明为:-

HashMap<Character,Integer> hs = new HashMap<>();

Then,key in map is of type Character data type and value of int type.Now,to get value corresponding to key irrespective of type of key,value type, syntax is :-

然后,map中的键为类型字符数据类型,值为int类型。现在,无论键的类型、值类型、语法如何,都要得到与键对应的值:-

    char temp = 'a';
    if(hs.containsKey(temp)){
`       int val = hs.get(temp); //val is the value corresponding to key temp
    }

So, according to your question you want to get string value corresponding to a key.For this, just declare HashMap as HashMap<"datatype of key","datatype of value" hs = new HashMap<>(); Using this will make your code cleaner and also you don't have to convert the result of hs.get("my_code") to string as by default it returns value of string if at entry time one has kept value as a string.

所以,根据你的问题,你想要得到对应于键的字符串值。为此,只需将HashMap声明为HashMap<" key的datatype "," value的datatype " hs = new HashMap<>();使用它可以使代码更简洁,而且您不必将hs.get(“my_code”)的结果转换为string,默认情况下,如果在输入时将值保留为string,则返回string的值。

#8


0  

You can use the get(Object key) method from the HashMap. Be aware that i many cases your Key Class should override the equals method, to be a useful class for a Map key.

您可以从HashMap中使用get(对象键)方法。注意,在很多情况下,您的键类应该重写equals方法,以便成为Map键的有用类。

#1


128  

Just use Map#get(key) ?

只是利用图#(关键)?

Object value = map.get(myCode);

Here's a tutorial about maps, you may find it useful: http://java.sun.com/docs/books/tutorial/collections/interfaces/map.html.

这里有一个关于地图的教程,您可能会发现它很有用:http://java.sun.com/docs/books/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

我希望看到一个字符串,如“ABC”或“DEF”,因为这是我最初放在那里的,但是如果我使用System.out.println(),就会得到类似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:

您将看到对象#toString()的结果。但. lang。String应该已经实现了一个,除非您创建了一个名称中小写s的自定义实现:java.lang.string。如果它实际上是一个自定义对象,那么您需要重写对象#toString(),以便在需要的对象上执行System.out.println()或toString()时获得“可读的字符串”。例如:

@Override
public String toString() {
    return "This is Object X with a property value " + value;
}

#2


48  

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)

问题是在HashMap (key & value)中填充什么

#3


8  

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.

如果这没有帮助,请澄清这个问题——最好是用一些代码来说明你的意思。

#4


8  

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

您将不会被要求输入map.get()或调用toString方法来获取字符串值。

#5


5  

map.get(myCode)

map.get(myCode)

#6


4  

An important point to be noted here is that if your key is an object of user-defined class in java then make it a point to override the equals method. Because the HashMap.get(Object key) method uses the equals method for matching the key value. If you do not override the equals method then it will try to find the value simply based on the reference of the key and not the actual value of key in which case it will always return a null.

这里要注意的一个重点是,如果您的键是java中用户定义类的对象,那么请将它设置为覆盖equals方法的点。因为HashMap。get(Object key)方法使用equals方法来匹配键值。如果您不重写equals方法,那么它将尝试根据键的引用而不是键的实际值来查找值,在这种情况下,它总是返回null。

#7


1  

Suppose you declared HashMap as :-

假设您将HashMap声明为:-

HashMap<Character,Integer> hs = new HashMap<>();

Then,key in map is of type Character data type and value of int type.Now,to get value corresponding to key irrespective of type of key,value type, syntax is :-

然后,map中的键为类型字符数据类型,值为int类型。现在,无论键的类型、值类型、语法如何,都要得到与键对应的值:-

    char temp = 'a';
    if(hs.containsKey(temp)){
`       int val = hs.get(temp); //val is the value corresponding to key temp
    }

So, according to your question you want to get string value corresponding to a key.For this, just declare HashMap as HashMap<"datatype of key","datatype of value" hs = new HashMap<>(); Using this will make your code cleaner and also you don't have to convert the result of hs.get("my_code") to string as by default it returns value of string if at entry time one has kept value as a string.

所以,根据你的问题,你想要得到对应于键的字符串值。为此,只需将HashMap声明为HashMap<" key的datatype "," value的datatype " hs = new HashMap<>();使用它可以使代码更简洁,而且您不必将hs.get(“my_code”)的结果转换为string,默认情况下,如果在输入时将值保留为string,则返回string的值。

#8


0  

You can use the get(Object key) method from the HashMap. Be aware that i many cases your Key Class should override the equals method, to be a useful class for a Map key.

您可以从HashMap中使用get(对象键)方法。注意,在很多情况下,您的键类应该重写equals方法,以便成为Map键的有用类。