Java 将哈希映射键与字符串进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19019052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare hash map key with the string
提问by user2817712
I want to compare the string with the key from the hashmap. I tried to use steps mentioned here Compare map key with a list of stringsbut didn't work for me.
我想将字符串与哈希图中的键进行比较。我尝试使用此处提到的步骤将映射键与字符串列表进行比较,但对我不起作用。
The hashmap contains many entries and want to compare the string which I am passing. If the key matches the string it should stop there and print the value of matched string. Below is my code :
哈希图包含许多条目,并且想要比较我传递的字符串。如果键匹配字符串,它应该停在那里并打印匹配字符串的值。下面是我的代码:
HashMap<String, MyBO> myObjs = MyData.getMyData();
Set<String> keys = myObjs.keySet();
String id = "ABC";
for(String code: keys) {
MyBO bo = myObjs.get(code);
if(keys.contains(itemId)) {
System.out.println("Matched key = " + id);
} else {
System.out.println("Key not matched with ID");
}
}
采纳答案by Ruchira Gayan Ranaweera
This will work for you
这对你有用
HashMap<String, MyBO> myObjs = MyData.getMyData();
String id = "ABC";
if(myObjs.containsKey(id)){
System.out.println("Matched key = " + id);
} else{
System.out.println("Key not matched with ID");
}
For example consider following example
例如考虑以下示例
HashMap<String, String> myObjs =new HashMap<>();
myObjs.put("ABC","a");
myObjs.put("AC","a");
String id = "ABC";
if(myObjs.containsKey(id)){
System.out.println("Matched key = " + id);
} else{
System.out.println("Key not matched with ID");
}
Out put.
输出。
Matched key = ABC
回答by Rick Hanlon II
Here's what you're looking for, note the differences with your code:
这是您要查找的内容,请注意与您的代码的不同之处:
HashMap<String, MyBO> myObjs = MyData.getMyData();
Set<String> keys = myObjs.keySet();
String id = "ABC";
for(String code: keys) {
if(code.equals(id) { /* this compares the string of the key to "ABC" */
System.out.println("Matched key = " + id);
} else {
System.out.println("Key not matched with ID");
}
}
Alternatively, though, you can do:
或者,您可以执行以下操作:
HashMap<String, MyBO> myObjs = MyData.getMyData();
Set<String> keys = myObjs.keySet();
if(keys.contains("ABC") { /* this checks the set for the value "ABC" */
System.out.println("Matched key = ABC");
} else {
System.out.println("Key not matched with ID");
}
}
回答by Rogalla Fabian
Try this code and resemble it with your code requirement it w ill work for you
试试这个代码,并把它与你的代码要求相似,它会为你工作
for (String key:keys){
String value = mapOfStrings.get(key);
//here it must uderstand, that the inputText contains "java" that equals to
//the key="java" and put in outputText the correspondent value
if (inputText.contains(key))
{
outputText = value;
}
}
回答by raduns
Try this way,
试试这个方法
HashMap<String, String> dataArr = new HashMap<>();
dataArr.put("Key 1", "First String");
String keyValueStr = dataArr.keySet().toString();
String matchValueStr = "Key 1";
//System.out.println(keyValueStr);
if(keyValueStr.equals("["+matchValueStr+"]"))
System.out.println("Match Found");
else
System.out.println("No Match Found");