Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Collection;

import java.util.Map;

import java.util.Set;

public class Main {
    /**
     * Collections are equals if they contain the same elements 
     * independent of ordering.
     * To this to be true, they must have the same number of elements.
     * Empty collections are equal.
     * 
     * @param <T>
     * @param c1
     * @param c2
     * @return
     */
    public static <T> boolean equalContents(Collection<? extends T> c1, Collection<? extends T> c2) {

        if (c1 == c2) {
            return true;
        } else if (c1.isEmpty() && c2.isEmpty()) {
            return true;
        }
        if (c1.size() != c2.size()) {
            return false;
        }

        if (!(c2 instanceof Set) && (c1 instanceof Set || c1.size() > c2.size())) {
            //swap
            Collection<? extends T> tmp = c1;
            c1 = c2;
            c2 = tmp;
        }

        for (T t : c1) {
            if (!c2.contains(t)) {
                return false;
            }
        }
        return true;

    }

    /**
     * Determine if the contents of two maps are the same, i.e. the same keys are mapped to the same values in both maps.
     * @param a the first {@link Map}
     * @param b the secound {@link Map}
     * @return <code>true</code> if the contents are the same, <code>false</code> otherwise.
     */
    public static <K, V> boolean equalContents(Map<? extends K, ? extends V> a, Map<? extends K, ? extends V> b) {
        if (a == b) {
            return true;
        } else if (a.isEmpty() && b.isEmpty()) {
            return true;
        }
        if (a.size() != b.size()) {
            return false;
        } else {

            for (Map.Entry entryA : a.entrySet()) {

                if (!b.containsKey(entryA.getKey()) || !b.get(entryA.getKey()).equals(entryA.getValue())) {
                    return false;
                }

            }

            return true;
        }

    }
}