Java List Compare compareLists(List list1, List list2)

Here you can find the source of compareLists(List list1, List list2)

Description

Provides a consistent ordering over lists.

License

Open Source License

Declaration

public static <T extends Comparable<T>> int compareLists(List<T> list1,
        List<T> list2) 

Method Source Code

//package com.java2s;

import java.util.List;

public class Main {
    /**//from  w  w w. ja  v a2  s .  c o m
     * Provides a consistent ordering over lists. First compares by the first
     * element. If that element is equal, the next element is considered, and so
     * on.
     */
    public static <T extends Comparable<T>> int compareLists(List<T> list1,
            List<T> list2) {
        if (list1 == null && list2 == null)
            return 0;
        if (list1 == null || list2 == null) {
            throw new IllegalArgumentException();
        }
        int size1 = list1.size();
        int size2 = list2.size();
        int size = Math.min(size1, size2);
        for (int i = 0; i < size; i++) {
            int c = list1.get(i).compareTo(list2.get(i));
            if (c != 0)
                return c;
        }
        if (size1 < size2)
            return -1;
        if (size1 > size2)
            return 1;
        return 0;
    }
}

Related

  1. compareLists(List strings1, List strings2, boolean ignoreCase)
  2. compareLists(List l1, List l2)
  3. compareLists(List list, List> listOfLists)
  4. compareLists(List a, List b)
  5. compareLists(List a, List b)
  6. compareLists(Object obj1, Object obj2)
  7. compareLists(String list1[], String list2[])
  8. compareListsAndNull(List arg1, List arg2)
  9. comparer(Comparator comparator, T object, T... withList)