Java Collection Equal equal(Collection i1, Collection i2)

Here you can find the source of equal(Collection i1, Collection i2)

Description

Equal.

License

Open Source License

Parameter

Parameter Description
i1 the i1
i2 the i2

Return

true, if successful

Declaration

public final static boolean equal(Collection i1, Collection i2) 

Method Source Code


//package com.java2s;

import java.util.Arrays;
import java.util.Collection;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Main {
    /**/* w w w  .j  a v  a 2s  .com*/
     * Checks if two Objects are equal.
     *
     * @param o1 the o1
     * @param o2 that Object
     * @return <code>true</code> if aThis and aThat are equal, <code>false</code> otherwise.
     */
    public final static boolean equal(Object o1, Object o2) {
        return o1 == o2 ? true : o1 != null ? o1.equals(o2) : false;
    }

    /**
     * Are equal for Integer values.
     *
     * @param l1 the l1
     * @param l2 the l2
     * @return true, if successful
     */
    public final static boolean equal(Long l1, Long l2) {
        return l1 != null && l2 != null ? l1.longValue() == l2.longValue() : l1 == l2;
    }

    /**
     * Checks if two Arrays are equal.
     * @param o1 this Arrays of objects
     * @param o2 that Arrays of objects
     * @return <code>true</code> if aThis and aThat are equal, <code>false</code> otherwise.
     */
    public final static boolean equal(Object[] o1, Object[] o2) {
        return Arrays.equals(o1, o2);
    }

    /**
     * Checks if two Map's.
     *
     * @param m1 the a this
     * @param m2 the a that
     * @return true, if successful
     */
    public final static boolean equal(Map m1, Map m2) {
        if (null == m1)
            return (null == m2);
        if (m1 == m2)
            return true;
        if (m1.size() != m2.size())
            return false;
        boolean ret = true;

        Iterator it = m1.entrySet().iterator();
        while (it.hasNext() && ret) {
            Map.Entry entry = (Map.Entry) it.next();
            ret = equal(entry.getValue(), m2.get(entry.getKey()));
        }
        return ret;
    }

    /**
     * Equal.
     *
     * @param s1 the s1
     * @param s2 the s2
     * @return true, if successful
     */
    public static boolean equal(Set s1, Set s2) {
        return s1 == s2 ? true : s1 == null || s2 == null ? false : s1.size() == s2.size() && s1.containsAll(s2);
    }

    /**
     * Equal.
     *
     * @param i1 the i1
     * @param i2 the i2
     * @return true, if successful
     */
    public final static boolean equal(Collection i1, Collection i2) {
        if (i1 == i2)
            return true;
        if (null == i1 || null == i2)
            return i1 == i2;
        if (i1.size() != i2.size())
            return false;

        final Iterator it1 = i1.iterator();
        final Iterator it2 = i2.iterator();
        boolean r1, r2, ret = true;

        do {
            r1 = it1.hasNext();
            r2 = it2.hasNext();
            ret = ret && (r1 == r2) && (r1 ? equal(it1.next(), it2.next()) : true);
        } while (ret && r1);

        return ret;
    }

    /**
     * Checks if two Iterable objects, such as Collections, Lists, Sets are equal.
     *
     * @param i1 the a this
     * @param i2 the a that
     *
     * @return true, if successful
     */
    public final static boolean equal(Iterable i1, Iterable i2) {

        if (i1 == i2)
            return true;
        if (null == i1 || null == i2)
            return i1 == i2;

        final Iterator it1 = i1.iterator();
        final Iterator it2 = i2.iterator();
        boolean r1, r2, ret = true;

        do {
            r1 = it1.hasNext();
            r2 = it2.hasNext();
            ret = ret && (r1 == r2) && (r1 ? equal(it1.next(), it2.next()) : true);
        } while (ret && r1);

        return ret;

    }

    /**
     * Equals method for Strings or CharSequence objects.
     *
     * @param s1 the a this
     * @param s2 the a that
     * @return true, if successful
     */
    public final static boolean equal(CharSequence s1, CharSequence s2) {
        return s1 == s2 ? true : s1 != null ? s1.equals(s2) : false;
    }

    /**
     * Equal.
     *
     * @param s1 the s1
     * @param s2 the s2
     * @return true, if successful
     */
    public final static boolean equal(String o1, String o2) {
        return o1 == o2 ? true : o1 != null ? o1.equals(o2) : false;
    }

    /**
     * Equal for arrays with numbers (Double, Float, Integer, Long, etc).
     *
     * @param n1 the a this
     * @param n2 the a that
     * @return true, if successful
     */
    public final static boolean equal(Number[] n1, Number[] n2) {
        if (null == n1)
            return (null == n2);

        if (n1.length != n2.length)
            return false;
        boolean ret = true;
        for (int i = 0; ret && i < n1.length; i++)
            ret = equal(n1[i], n2[i]);
        return ret;
    }

    /**
     * Checks if two Objects are equal.
     * @param check if <code>true</code> check is performed, otherwise <code>false</code> is returned without a check
     * @param aThis this Object
     * @param aThat that Object
     * @return <code>true</code> if aThis and aThat are equal, <code>false</code> otherwise.
     */
    static public boolean equal(boolean check, Object aThis, Object aThat) {
        return check && equal(aThis, aThat);
    }

    /**
     * Equal.
     *
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @return true, if successful
     */
    public final static boolean equal(double[] arg1, double[] arg2) {
        return Arrays.equals(arg1, arg2);
    }

    /**
     * Equal.
     *
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @return true, if successful
     */
    public final static boolean equal(float[] arg1, float[] arg2) {
        return Arrays.equals(arg1, arg2);
    }

    /**
     * Equal.
     *
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @return true, if successful
     */
    public final static boolean equal(long[] arg1, long[] arg2) {
        return Arrays.equals(arg1, arg2);
    }

    /**
     * Equal.
     *
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @return true, if successful
     */
    public final static boolean equal(int[] arg1, int[] arg2) {
        return Arrays.equals(arg1, arg2);
    }

    /**
     * Equal.
     *
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @return true, if successful
     */
    public final static boolean equal(boolean[] arg1, boolean[] arg2) {
        return Arrays.equals(arg1, arg2);
    }
}

Related

  1. containsAtleastOneEqualString(Collection left, String right)
  2. containsByEquals(Collection collection, Object obj)
  3. content_equality(Collection A, Collection B)
  4. contentEquals(Collection l1, Collection l2)
  5. contentEquals(Collection list1, Collection list2)
  6. equal(Collection col1, Collection col2)
  7. equalCollection(Collection c1, Collection c2)
  8. equalContent(Collection col1, Collection col2)
  9. equalContent(final Collection a, final Collection b)