Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Map;

public class Main {
    /**
     * assert the two map whether equals or not?
     * first: if two maps all null , return true.
     * second: if one of maps is null return false;
     * three : if the two maps' s size are not ==, return false;
     * four : every value with the key in map1,and with the key to 
     * search the map2, if map2'value is null or not equals , 
     * return false;
     * 
     * @param map1
     * @param map2
     * @return
     */
    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;
    }

    /**
     * assert two objects whether equals or not?
     * @param obj1
     *           the obj1 argument.
     * @param obj2
     *           the obj2 argument;
     * @return
     *         flag .
     */
    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);
    }
}