【问题标题】:Java - Verify if a key from an hashmap exist in the other hashmap [closed]Java - 验证来自哈希映射的键是否存在于另一个哈希映射中[关闭]
【发布时间】:2020-08-03 14:15:53
【问题描述】:

如何验证来自 hashmap 的键是否存在于其他 hashmap 中。

我有两个哈希图

tablePlayer: <"Kim":"1" , "Bot":"2" , "Tim":"3">
tempPlayerList: <"Kim":"4" , "Bob":"1">

我的想法是使用tempPlayerList 键来查找是否有任何具有相同值的键,例如,Kim 应该是 true 而 Bob 应该是 false

有什么办法吗?

【问题讨论】:

  • 你有没有尝试过?例如,您可以遍历tablePlayer#keySet 以找到使templatePlayerList#containsKey 返回true 的那些。许多实现可能:)

标签: java


【解决方案1】:

我真的不明白你想要什么,但你可以做:

for(String playerName : tablePlayer.keySet()){
    if(tempPlayerList.containsKey(playerName)){
        //hasmap contain name
    }else{
        //hasmap do not contain name
    }
}

【讨论】:

  • 我尝试了这个解决方案,但它仍然没有得到 if 部分只有 else(使用 Kim;4)for(String playerName : tempPlayerList.keySet()){ if(tablePlayer.containsKey(playerName)){ System.out.println ("true"); } else { System.out.println ("false"); } }
猜你喜欢
  • 1970-01-01
  • 2013-11-23
  • 1970-01-01
  • 2015-08-31
  • 1970-01-01
  • 2018-10-13
  • 1970-01-01
  • 1970-01-01
  • 2014-09-16
相关资源
最近更新 更多