Java Collection Compare compareAnyOrder(Collection c1, Collection c2)

Here you can find the source of compareAnyOrder(Collection c1, Collection c2)

Description

compare Any Order

License

Open Source License

Declaration

public static boolean compareAnyOrder(Collection<?> c1, Collection<?> c2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.util.Set;

public class Main {
    public static boolean compareAnyOrder(Collection<?> c1, Collection<?> c2) {
        if (c1.size() != c2.size())
            return false;
        for (Iterator<?> i1 = c1.iterator(); i1.hasNext();)
            if (!c2.contains(i1.next()))
                return false;
        return true;
    }//from w w  w .j a va  2 s .c o m

    public static boolean contains(Object[] children, Object child) {
        if (children == null)
            return false;
        for (int i = 0; i < children.length; i++)
            if ((children[i] == child) || ((children[i] != null) && children[i].equals(child)))
                return true;
        return false;
    }

    public static boolean equals(Object[] objs1, Object[] objs2) {
        if ((objs1 == null) && (objs2 == null))
            return true;
        if ((objs1 == null) || (objs2 == null))
            return false;
        if (objs1.length != objs2.length)
            return false;
        Set<Object> set1 = toSet(objs1);
        Set<Object> set2 = toSet(objs2);
        set1.removeAll(set2);
        return set1.size() == 0;
    }

    /**
     * @param selection
     * @return
     */
    public static Set<Object> toSet(Object[] objs) {
        Set<Object> ret = new HashSet<Object>();
        if (objs != null)
            for (int i = 0; i < objs.length; i++)
                ret.add(objs[i]);
        return ret;
    }

    public static void removeAll(Collection<Object> ret, Object[] elements) {
        if (elements != null)
            for (int i = 0; i < elements.length; i++)
                ret.remove(elements[i]);
    }

    public static void removeAll(Collection<Object> ret, Iterator<Object> elements) {
        if (elements != null)
            while (elements.hasNext())
                ret.remove(elements.next());
    }
}

Related

  1. compare(Collection a, Collection b)
  2. compare(Collection a, Collection b)
  3. compare(Collection lhs, Collection rhs)
  4. compare(final Collection o1, final Collection o2)
  5. compare(final Collection c0, final Collection c1, final Comparator c)
  6. compareCollections(Collection a, Collection b)
  7. compareCollections(Collection cola, Collection colb)
  8. compareCollections(Collection value1, Collection value2)
  9. compareExactOrder(Collection c1, Collection c2)