Good catch! I actually had that in my original code, but when I "cleaned" it
up I removed it for what ever reason.

Updated version:

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

* @author James R Perkins (JRP)
* @version 1.0.0
public class CollectionsUtil {
private CollectionsUtil() {


* Compares the first collection against the second collection. Returns
* negative integer, zero or positive integer.
* <p>
* If the size of the first collection is greater than the size of the
* second collection, a positive integer is return. If the size of the
* collection is less than the size of the second collection a negative
* number is returned.
* </p>
* <p>
* If the size of the two collections is equal, then each element from
* collection 1 is compared to the element in the same position from
* collection 2.
* </p>
* @param <T>
* the type of the collection. Both collections must be the
* type and implement {@code java.lang.Comparable}.
* @param c1
* the first collection to be compared
* @param c2
* the second collection to be compared
* @return a negative integer, zero, or a positive integer as the first
* argument is less than, equal to, or greater than the second.
public static <T extends Comparable<? super T>> int compare(
Collection<T> c1, Collection<T> c2) {
int c = c1.size() - c2.size();
if (c != 0)
return c;
List<T> l1 = new ArrayList<T>(c1);
List<T> l2 = new ArrayList<T>(c2);
for (int i = 0; i < l1.size(); i++) {
c = l1.get(i).compareTo(l2.get(i));
if (c != 0) {
return c;

James R. Perkins

On Wed, Sep 2, 2009 at 16:13, M. Lazarus <mlazarus@xxxxxxxx> wrote:


Why not do the test for an unequal number of elements, and execute
a return, before entering the loop?


Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2015 by MIDRANGE dot COM and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available here. If you have questions about this, please contact