英文:
Java - Verify if a key from an hashmap exist in the other hashmap
问题
你可以使用以下方法验证一个哈希映射中的键是否存在于另一个哈希映射中:
# 给定的两个哈希映射
tablePlayer = {"Kim": "1", "Bot": "2", "Tim": "3"}
tempPlayerList = {"Kim": "4", "Bob": "1"}
# 验证tempPlayerList中的键是否存在于tablePlayer中,并将结果存储在一个新的字典中
result = {key: key in tablePlayer for key in tempPlayerList}
# 打印结果
print(result)
这将返回一个包含每个键是否存在于tablePlayer中的布尔值的字典,例如,Kim对应的值为True,Bob对应的值为False。
英文:
How can I verify if a key from a hashmap exist in other hashmap.
I've two hashmaps
tablePlayer: <"Kim":"1" , "Bot":"2" , "Tim":"3">
tempPlayerList: <"Kim":"4" , "Bob":"1">
And my idea was to use tempPlayerList
keys to find if there any key with the same value, e.g., Kim should be true and Bob should be false
Is there any way to do this?
答案1
得分: 1
我不太明白你想要什么,但你可以这样做:
for(String playerName : tablePlayer.keySet()){
if(tempPlayerList.containsKey(playerName)){
// 哈希表包含姓名
}else{
// 哈希表不包含姓名
}
}
<details>
<summary>英文:</summary>
I dont realy understand what you want but you can do :
for(String playerName : tablePlayer.keySet()){
if(tempPlayerList.containsKey(playerName)){
//hasmap contain name
}else{
//hasmap do not contain name
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论