Java Map Equal mapEquals(Map map1, Map map2)

Here you can find the source of mapEquals(Map map1, Map map2)

Description

map Equals

License

Apache License

Declaration

public static boolean mapEquals(Map<?, ?> map1, Map<?, ?> map2) 

Method Source Code

//package com.java2s;
/*/*from www . j  a v  a 2s  .c  o  m*/
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Map;

public class Main {
    public static boolean mapEquals(Map<?, ?> map1, Map<?, ?> map2) {
        if (map1 == null && map2 == null) {
            return true;
        }
        if (map1 == null || map2 == null) {
            return false;
        }
        if (map1.size() != map2.size()) {
            return false;
        }
        for (Map.Entry<?, ?> entry : map1.entrySet()) {
            Object key = entry.getKey();
            Object value1 = entry.getValue();
            Object value2 = map2.get(key);
            if (!objectEquals(value1, value2)) {
                return false;
            }
        }
        return true;
    }

    private static boolean objectEquals(Object obj1, Object obj2) {
        if (obj1 == null && obj2 == null) {
            return true;
        }
        if (obj1 == null || obj2 == null) {
            return false;
        }
        return obj1.equals(obj2);
    }
}

Related

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