Java Collection Compare compare(final Collection c0, final Collection c1, final Comparator c)

Here you can find the source of compare(final Collection c0, final Collection c1, final Comparator c)

Description

Compares two Collections according to a comparator.

License

Open Source License

Parameter

Parameter Description
c0 the first collection
c1 the second collection
c the comparator

Exception

Parameter Description
NullPointerException if the comparator is <code>null</code>

Return

true if the two collections are equal according to the comparator, otherwise false

Declaration

public static <Type> boolean compare(final Collection<? extends Type> c0, final Collection<? extends Type> c1,
        final Comparator<Type> c) 

Method Source Code


//package com.java2s;
/*//from  ww  w.  j  a v  a2s  .  c  o m
 * Integrated Rule Inference System (IRIS):
 * An extensible rule inference system for datalog with extensions.
 * 
 * Copyright (C) 2008 Semantic Technology Institute (STI) Innsbruck, 
 * University of Innsbruck, Technikerstrasse 21a, 6020 Innsbruck, Austria.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 */

import java.util.ArrayList;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

import java.util.List;

public class Main {
    /**
     * Compares two Collections according to a comparator.
     * 
     * @param c0
     *            the first collection
     * @param c1
     *            the second collection
     * @param c
     *            the comparator
     * @return <code>true</code> if the two collections are equal according to
     *         the comparator, otherwise <code>false</code>
     * @throws NullPointerException
     *             if one collection is <code>null</code>
     * @throws NullPointerException
     *             if the comparator is <code>null</code>
     * @since 0.3
     */
    public static <Type> boolean compare(final Collection<? extends Type> c0, final Collection<? extends Type> c1,
            final Comparator<Type> c) {
        if ((c0 == null) || (c1 == null)) {
            throw new NullPointerException("The collections must not be null");
        }
        if (c == null) {
            throw new NullPointerException("The comparator must not be null");
        }

        if (c0.size() != c1.size()) {
            return false;
        }

        final List<Type> l0 = new ArrayList<Type>(c0);
        final List<Type> l1 = new ArrayList<Type>(c1);
        Collections.sort(l0, c);
        Collections.sort(l1, c);

        for (final Iterator<Type> i0 = l0.iterator(), i1 = l1.iterator(); i0.hasNext();) {
            if (c.compare(i0.next(), i1.next()) != 0) {
                return false;
            }
        }
        return true;
    }
}

Related

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