Java List Sort isEqualList(final List list1, final List list2, Comparator c, boolean sortList)

Here you can find the source of isEqualList(final List list1, final List list2, Comparator c, boolean sortList)

Description

is Equal List

License

Open Source License

Declaration

public static <T> boolean isEqualList(final List<T> list1, final List<T> list2, Comparator<? super T> c,
            boolean sortList) 

Method Source Code

//package com.java2s;
/* /*from   w  ww.j  ava  2s.  co  m*/
 * Motu, a high efficient, robust and Standard compliant Web Server for Geographic
 * Data Dissemination.
 *
 * http://cls-motu.sourceforge.net/
 *
 * (C) Copyright 2009-2010, by CLS (Collecte Localisation Satellites) - 
 * http://www.cls.fr - and  Contributors
 *
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

import java.util.Collections;
import java.util.Comparator;

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

public class Main {
    public static <T> boolean isEqualList(final List<T> list1, final List<T> list2, Comparator<? super T> c,
            boolean sortList) {
        if (list1 == list2) {
            return true;
        }
        if (list1 == null || list2 == null || list1.size() != list2.size()) {
            return false;
        }

        if (sortList) {
            Collections.sort(list1, c);
            Collections.sort(list2, c);
        }

        Iterator<T> it1 = list1.iterator();
        Iterator<T> it2 = list2.iterator();
        T obj1 = null;
        T obj2 = null;

        while (it1.hasNext() && it2.hasNext()) {
            obj1 = it1.next();
            obj2 = it2.next();

            if ((obj1 == null) && (obj2 != null)) {
                return false;
            }
            if ((obj1 != null) && (obj2 == null)) {
                return false;
            }
            if (c.compare(obj1, obj2) != 0) {
                return false;
            }
        }

        return !(it1.hasNext() || it2.hasNext());
    }
}

Related

  1. getSortedList(Collection collection)
  2. getSortedList(Set items)
  3. getSortedStringList(List pathList)
  4. getZipfPlotData(List sortedCount)
  5. interpolateLinearlyQuantile(List sortedDataAscendingOrder, Double p)
  6. isSorted(Iterable list)
  7. isSorted(List list)
  8. isSorted(List list)
  9. isSorted(List> items, boolean asc)