1

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

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
4

1 回答 1

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}

于 2013-10-20T20:29:34.297 回答