Java Collection Compare compare(Collection c1, Collection c2)

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

Description

Compares two collections of Comparable elements.

License

Apache License

Declaration

@SuppressWarnings("rawtypes")
public static int compare(Collection<? extends Comparable> c1, Collection<? extends Comparable> c2) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**//w w  w . j a  v a2 s .  co  m
     *  Compares two collections of <code>Comparable</code> elements. The two collections are
     *  iterated, and the first not-equal <code>compareTo()</code> result is returned. If the
     *  collections are of equal length and contain the same elements in iteration order, they
     *  are considered equal. If they are of unequal length but contain the same elements in
     *  iteration order, the shorter is considered less than the longer.
     *  <p>
     *  Note that two collections that are equal based on their intrinsic <code>equals()</code>
     *  method, but iterate in a different order (ie, hash-based collections) are not considered
     *  equal by this method.
     *
     *   @since 1.0.14
     */
    @SuppressWarnings("rawtypes")
    public static int compare(Collection<? extends Comparable> c1, Collection<? extends Comparable> c2) {
        Iterator<? extends Comparable> itx1 = c1.iterator();
        Iterator<? extends Comparable> itx2 = c2.iterator();

        while (itx1.hasNext()) {
            if (!itx2.hasNext())
                return 1;

            Comparable v1 = itx1.next();
            Comparable v2 = itx2.next();
            int cmp = v1.compareTo(v2);
            if (cmp != 0)
                return cmp;
        }

        if (itx2.hasNext())
            return -1;
        else
            return 0;
    }
}

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)