【问题标题】:hashtable key-value pair comparison to find out unique pair哈希表键值对比较找出唯一对
【发布时间】:2020-04-04 13:48:19
【问题描述】:
public static void main(String[] args) {
Hashtable<String, String> ht1= new Hashtable<>();
ht1.put("10", ghu");
ht1.put("20", "lo");


Hashtable<String, String> ht2= new Hashtable<>();
ht2.put("10", "ko");
ht2.put("20", "lo");

如何通过使用键值对相互比较两个哈希表从两个哈希表中找到键值对的唯一条目..

预期输出... ("10", "ghu");这是来自第一个哈希表 ("10", "ko");这是来自第二个哈希表

【问题讨论】:

    标签: java comparison hashtable


    【解决方案1】:

    HashTable 保存一个条目,可以使用 getEntrySet() 方法提取该条目。 因此,您应该类似于下面的伪代码。您可能需要对以下 sn-p 进行一些更改以使其可编译。

    Set<Entry> entrySet= ht1.getEntrySet();//get all values
          //iterate over those
          for(Entry entry :entrySet ){
                String htVal = ht2.get(entry.getKey());
                 if (htval!=null && htVal.equals(entry.getValue())){
                    //non unique -ignore
                       }else{
                    //store ht1 and ht2 unique entries
                      }
                  }
    

    【讨论】:

    • 其实我想比较 ht1 条目集和不匹配的 ht2 条目集然后需要存储在第三个哈希表中
    • 您可以将两个 hashMap 的不匹配键值对存储在 else 块中。我的 IDE 没有安装在当前的笔记本电脑中,因此我输入了伪代码而不是完整代码。对此深表歉意。
    猜你喜欢
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2014-08-09
    • 1970-01-01
    • 2011-02-15
    • 2013-10-17
    相关资源
    最近更新 更多