1

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.

ntalbs
  • 28,700
  • 8
  • 66
  • 83
tinyhamster
  • 179
  • 1
  • 7
  • 3
    Do you want to know if there is a matching key, or do u want to know if the key and value matches exactly? By the way, you should use a HashMap, not HashTable (usually). – pczeus Mar 20 '16 at 22:34
  • I need to know if the key and value matches exactly. Java HashTable.contains method is not useful in this case. Also why should I use HashMap over HashTable? – tinyhamster Mar 20 '16 at 22:40
  • 1
    From the [Javadoc](https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html): "If a thread-safe implementation is not needed, it is recommended to use `HashMap` in place of `HashTable`." – bcsb1001 Mar 20 '16 at 22:46
  • There are some other minor differences, such as the ability to store `null`. See [Differences between HashMap and Hashtable?](http://stackoverflow.com/q/40471/1361506) for a more complete analysis of the differences. – azurefrog Mar 20 '16 at 22:54

2 Answers2

3

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.

e.g.

//  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
azurefrog
  • 10,785
  • 7
  • 42
  • 56
0

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
pczeus
  • 7,709
  • 4
  • 36
  • 51
  • What raw types are you referring to? – pczeus Mar 20 '16 at 22:51
  • `Map` is used as a raw type in the two methods of `HashMapMatch`. – bcsb1001 Mar 20 '16 at 22:53
  • Maybe because there is no dependency on the contents of the map? So you should place type information even when not needed? Why would you restrict a generic method to specific types? Ugh. – pczeus Mar 20 '16 at 22:55
  • It is far better practice to do something like `public boolean hasMatchingKey(K key, Map super K, ?> m1, Map super K, ?> m2)`. – bcsb1001 Mar 20 '16 at 23:00
  • Yup, there is a dependency on the key type. But man that method signature would be ugly and complicated for anyone new to Maps to comprehend. I chose to avoid that unnecessary complication for this trivial exercise. – pczeus Mar 20 '16 at 23:10