Eme*_*lTV 3 java string if-statement hashmap
这个问题听起来有点奇怪,但我可以解释一下:我有一个填充字符串作为键和值的 HashMap。我有一个字符串。现在我想做这样的事情:
if(myhashmap.containskey(ANY PART OF MY STRING)
Run Code Online (Sandbox Code Playgroud)
喜欢:
String str = "Java is cool!";
[HashMap that contains following : "Java";
Run Code Online (Sandbox Code Playgroud)
我希望我可以检查字符串是否包含某些内容。也许我可以用 for 循环来做到这一点,但没有像 hashmap.entry 这样的东西。你会怎么办?
您可以遍历地图的键并检查它们是否在字符串中:
String str = "Java is cool!";
Map<String, ...> map = ...;
for (String key : map.keySet()) {
if (str.contains(key)) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
如果您还需要对相关的值key,就可以使用entrySet。