【问题标题】:Program to find intersecting values from two hashtables从两个哈希表中查找相交值的程序
【发布时间】:2013-10-29 05:08:47
【问题描述】:

我有下面的代码,它有一个函数应该从两个哈希表中找到相交的值。任何人都可以看看我的代码并建议我应该怎么做才能让“相交”函数返回两个哈希表中的相交值。提前致谢。

import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;

public class hashtable {    
    public static void main(String[] args){ 
        Hashtable<String, Integer> hashtable = new Hashtable<String, Integer>();
        Hashtable<String, Integer> hashtable2 = new Hashtable<String, Integer>();

        hashtable.put("mike" , 1);
        hashtable.put("Lisa" ,2);
        hashtable.put("Louis" , 3);
        hashtable.put("Chris" ,4);
        hashtable.put("Chuck" , 5);
        hashtable.put("Kiril" ,6);

        /* table 2 values */    
        hashtable2.put("Louis" , 1);
        hashtable2.put("samy" ,2);
        hashtable2.put("Mo" , 3);
        hashtable2.put("lolo" , 4);
        hashtable2.put("Chuck" ,5);
        hashtable2.put("samual" ,6);

        System.out.println(hashtable);
        System.out.println(hashtable2);

        System.out.println("Below are intersecting Values");
        Intersect( hashtable , hashtable2 );
    }

    public static Hashtable<String, Integer> Intersect(
                                Hashtable<String, Integer> hashtable,
                                Hashtable<String, Integer> hashtable2){

        for (Entry<String, Integer> entry : hashtable.entrySet()) {

            if(hashtable2.contains(entry)){

                /*FYI, I tried  hashtable2.put(entry); but was not able to get it work*/
                hashtable2.putAll((Map<? extends String, ? extends Integer>)entry);
                System.out.println(hashtable2);
            }
        }

        return hashtable2;
    }
}

输出:

{Kiril=6, Lisa=2, Louis=3, Chris=4, mike=1, Chuck=5}
{samual=6, samy=2, Louis=1, lolo=4, Chuck=5, Mo=3}
Below are intersecting Values

【问题讨论】:

  • 那么你想要的预期结果是什么?根据值相交的两个哈希的键?请澄清
  • My for (Entry entry : hashtable.entrySet()) 有 String 和 key。例如我添加了这个 set .put("Chuck" ,5);认为我将能够找到 String 和 Key 的匹配项。但是,如果您认为仅按价值查找是一种理想的解决方案,那也很好。

标签: java algorithm data-structures hashtable


【解决方案1】:

您的输入值

    Hashtable<String, Integer> hashtable = new Hashtable<String, Integer>();
    Hashtable<String, Integer> hashtable2 = new Hashtable<String, Integer>();

    hashtable.put("mike" , 1);
    hashtable.put("Lisa" ,2);
    hashtable.put("Louis" , 3);
    hashtable.put("Chris" ,4);
    hashtable.put("Chuck" , 5);
    hashtable.put("Kiril" ,6);

    /* table 2 values */    
    hashtable2.put("Louis" , 1);
    hashtable2.put("samy" ,2);
    hashtable2.put("Mo" , 3);
    hashtable2.put("lolo" , 4);
    hashtable2.put("Chuck" ,5);
    hashtable2.put("samual" ,6);

在这里过滤值

Set<String> s= new HashSet<String>();
s.addAll(hashtable.keySet());
s.retainAll(hashtable2.keySet());
System.out.println(s);
Hashtable<String, Integer> inersect = new Hashtable<String, Integer>();
for (Entry<String, Integer> entry :hashtable.entrySet()) {
    if (s.contains(entry.getKey())) {
        inersect.put(entry.getKey(), entry.getValue());
    }             
}
System.out.println(inersect);

您可以这样做的最简单方法。

Hashtable<String, Integer> inersect = new Hashtable<String, Integer>(hashtable);
inersect.keySet().retainAll(hashtable2.keySet());
System.out.println(inersect);

输出

{路易斯=3,查克=5}

【讨论】:

    猜你喜欢
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    相关资源
    最近更新 更多