编程以从两个哈希表中查找相交值

时间:2013-10-20 19:58:31

标签: java algorithm data-structures hashtable

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

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

1 个答案:

答案 0 :(得分:2)

您的输入值

    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}