Java List Compare compareLists(List a, List b)

Here you can find the source of compareLists(List a, List b)

Description

Compare two lists of things which extend the same type .

License

Apache License

Declaration

public static <T extends Comparable<T>> int compareLists(List<T> a, List<T> b) 

Method Source Code


//package com.java2s;
/*//  ww w  . java 2s .c  om
 * Copyright (c) 2015 Spotify AB.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * Compare two lists of things which extend the same type <T>.
     */
    public static <T extends Comparable<T>> int compareLists(List<T> a, List<T> b) {
        final Iterator<T> left = a.iterator();
        final Iterator<T> right = b.iterator();

        if (left.hasNext()) {
            if (!right.hasNext()) {
                return -1;
            }

            final T l = left.next();
            final T r = right.next();

            final int c = l.compareTo(r);

            if (c != 0) {
                return c;
            }
        }

        if (right.hasNext()) {
            return 1;
        }

        return 0;
    }
}

Related

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