简体   繁体   中英

How to check if java hashtable contains key-value pairs from another hashtable

Is there any built-in java method to check if one hash table contains all key-value pairs that exist in the other one?

For example:

h1={{0,1},{1,4},{2,5},{3,6}}
h2={{0,1},{2,5}}

In this case, it is true that h1 contains key-value pairs from h2 .

There's no method on Hashtable that lets you directly check this, but you can use the entrySet() method on each Hashtable to obtain a Set all of its key-value pairs.

Then you can just use containsAll() to see if one of them is a subset of the other, since it

Returns true if this set contains all of the elements of the specified collection. If the specified collection is also a set, this method returns true if it is a subset of this set.

eg

//  h1={{0,1},{1,4},{2,5},{3,6}}
    Hashtable<Integer, Integer> h1 = new Hashtable<>();
    h1.put(0, 1);
    h1.put(1, 4);
    h1.put(2, 5);
    h1.put(3, 6);

//  h2={{0,1},{2,5}}
    Hashtable<Integer, Integer> h2 = new Hashtable<>();
    h2.put(0, 1);
    h2.put(2, 5);

    Set<Entry<Integer, Integer>> e1 = h1.entrySet();
    Set<Entry<Integer, Integer>> e2 = h2.entrySet();

    System.out.println(e2.containsAll(e1));  // false
    System.out.println(e1.containsAll(e2));  // true

Here is a small sample program to demonstrate how to match keys or key and values of 2 different maps:

    public class HashMapMatch {

        public boolean hasMatchingKey(String key, Map m1, Map m2){
            return m1.containsKey(key) && m2.containsKey(key);
        }

        public boolean hasMatchingKeyAndValue(String key, Map m1, Map m2){
            if(hasMatchingKey(key, m1, m2)){
                return m1.get(key).equals(m2.get(key));
            }
            return false;
        }

        public static void main(String[] args) {
            HashMapMatch matcher = new HashMapMatch();
            Map<String, Integer> map1 = new HashMap<>();
            map1.put("One",1);
            map1.put("Two",22);
            map1.put("Three",3);

            Map<String, Integer> map2 = new HashMap<>();
            map2.put("One",1);
            map2.put("Two",2);
            map2.put("Four",4);

            System.out.println("Has matching key? :" + matcher.hasMatchingKey("One", map1, map2));
            System.out.println("Has matching key? :" + matcher.hasMatchingKey("Two", map1, map2));
            System.out.println("Has matching key? :" + matcher.hasMatchingKey("Three", map1, map2));

            System.out.println("Has matching key value: :" + matcher.hasMatchingKeyAndValue("One", map1,map2));
            System.out.println("Has matching key value: :" + matcher.hasMatchingKeyAndValue("Two", map1,map2));

        }
    }

Produces the output:

Has matching key? :true
Has matching key? :true
Has matching key? :false
Has matching key value: :true
Has matching key value: :false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM