× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



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
a
* 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
first
* 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
same
* 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) {
break;
}
}
return c;
}
}

--
James R. Perkins
http://twitter.com/the_jamezp


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

James,

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

-mark


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.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 on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.