Java Map Equal mapEquals(Map a, Object b)

Here you can find the source of mapEquals(Map a, Object b)

Description

map Equals

License

Open Source License

Declaration

public static <K, V> boolean mapEquals(Map<K, V> a, Object b) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;
import java.util.Map.Entry;

public class Main {
    public static <K, V> boolean mapEquals(Map<K, V> a, Object b) {
        if (a == b)
            return true;
        if (b == null || a == null)
            return false;
        if (!(b instanceof Map))
            return false;
        Map<?, ?> t = (Map<?, ?>) b;
        if (a.size() != t.size())
            return false;
        try {/*from ww  w.  j a  v a 2  s  . c om*/
            for (Entry<K, V> e : a.entrySet()) {
                K key = e.getKey();
                V value = e.getValue();
                if (value == null) {
                    if (!(t.get(key) == null && t.containsKey(key)))
                        return false;
                } else {
                    if (!value.equals(t.get(key)))
                        return false;
                }
            }
        } catch (ClassCastException unused) {
            return false;
        } catch (NullPointerException unused) {
            return false;
        }
        return true;
    }
}

Related

  1. mapEquals(Map fst, Map snd)
  2. mapEquals(Map map1, Map map2)
  3. mapEquals(Map map1, Map map2)
  4. mapEquals(Map leftMap, Map rightMap)
  5. MapEquals(Map mapA, Map mapB)