Java Collection Equal equalsCollectionsAnyOrder(Collection i1, Collection i2)

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

Description

Check if values in iterables are identical (in any order) This method check sizes first For first collection calls Iterable#iterator() only once, and several times for second collection (max = size of collection)

License

Open Source License

Declaration

public static boolean equalsCollectionsAnyOrder(Collection<?> i1,
        Collection<?> i2) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * The MIT License (MIT)//w w  w.j  a  v  a 2 s.  c  o  m
 * 
 * Copyright (c) 2016 Dalibor Drgo? <emptychannelmc@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**
     * Check if values in iterables are identical (in any order)
     * This method check sizes first
     * For first collection calls Iterable#iterator() only once, and
     * several times for second collection (max = size of collection)
     */
    public static boolean equalsCollectionsAnyOrder(Collection<?> i1,
            Collection<?> i2) {
        if (i1 == i2) {
            return true;
        }
        if (i1 == null) {
            if (i2 == null) {
                return true;
            }
            return false;
        } else if (i2 == null) {
            return false;
        }
        int size = i1.size();
        if (size != i2.size()) {
            return false;
        }
        boolean[] was = new boolean[size];
        Iterator<?> it1 = i1.iterator();
        for (int i = 0; i < size; i++) {
            if (!it1.hasNext()) {
                return false;
            }
            Object cur = it1.next();
            Iterator<?> it2 = i2.iterator();
            boolean wasCur = false;
            if (cur == null) {
                for (int seci = 0; seci < size; seci++) {
                    if (!it2.hasNext()) {
                        return false;
                    }
                    Object cur2 = it2.next();
                    if (!was[seci]) {
                        if (cur2 == null) {
                            was[seci] = true;
                            wasCur = true;
                            break;
                        }
                    }
                }
            } else {
                for (int seci = 0; seci < size; seci++) {
                    if (!it2.hasNext()) {
                        return false;
                    }
                    Object cur2 = it2.next();
                    if (!was[seci]) {
                        if (cur.equals(cur2)) {
                            was[seci] = true;
                            wasCur = true;
                            break;
                        }
                    }
                }
            }
            if (!wasCur || it2.hasNext()) {
                return false;
            }
        }
        return !it1.hasNext();
    }

    public static boolean equals(Object[] one, Object[] two, int size) {
        if (one == two) {
            return true;
        }
        while (size-- != 0) {
            Object on = one[size];
            Object tw = two[size];
            if ((on == null) ? tw != null : !on.equals(tw)) {
                return false;
            }
        }
        return true;
    }

    public static boolean equals(Object[] one, int off1, Object[] two,
            int off2, int size) {
        if (one == two && off1 == off2) {
            return true;
        }
        size += off1;
        for (; off1 < size; off1++, off2++) {
            Object on = one[off1];
            Object tw = two[off2];
            if ((on == null) ? tw != null : !on.equals(tw)) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. equals(Collection collection1, Collection collection2)
  2. equals(Collection a, Collection b, boolean ordered)
  3. equals(final Collection c1, final Collection c2)
  4. equals(Iterable collection1, Iterable collection2)
  5. equalsAny(Object object, Collection objects)
  6. equalsNull(Collection collection)
  7. equalsOne(final T o1, final Collection others)
  8. equalsSet(Collection c1, Collection c2)
  9. equalsSet(Collection c1, Collection c2)

  10. HOME | Copyright © www.java2s.com 2016