compare two list isEquals(null, null) = true; - Android java.util

Android examples for java.util:List Element

Description

compare two list isEquals(null, null) = true;

Demo Code


//package com.java2s;
import java.util.ArrayList;

public class Main {
    /**//from w w  w.ja  v  a2s.c  om
     * compare two list
     * 
     * <pre>
     * isEquals(null, null) = true;
     * isEquals(new ArrayList&lt;String&gt;(), null) = false;
     * isEquals(null, new ArrayList&lt;String&gt;()) = false;
     * isEquals(new ArrayList&lt;String&gt;(), new ArrayList&lt;String&gt;()) = true;
     * </pre>
     * 
     * @param <V>
     * @param actual
     * @param expected
     * @return
     */
    public static <V> boolean isEquals(ArrayList<V> actual,
            ArrayList<V> expected) {
        if (actual == null) {
            return expected == null;
        }
        if (expected == null) {
            return false;
        }
        if (actual.size() != expected.size()) {
            return false;
        }

        for (int i = 0; i < actual.size(); i++) {
            if (actual.get(i) == expected.get(i)
                    || (actual.get(i) == null ? expected.get(i) == null
                            : actual.get(i).equals(expected.get(i)))) {
                return false;
            }
        }
        return true;
    }
}

Related Tutorials