Java List Compare compareStringList(List list1, List list2)

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

Description

Compare the given lists of strings.

License

Open Source License

Parameter

Parameter Description
list1 an list of strings
list2 an other list of strings

Return

true if the lists are equivalents, false otherwise

Declaration

public static boolean compareStringList(List<String> list1, List<String> list2) 

Method Source Code

//package com.java2s;
/**/* ww  w  .  j  a  v  a 2s.c o m*/
 * AADL-Utils
 * 
 * Copyright ? 2012 TELECOM ParisTech and CNRS
 * 
 * TELECOM ParisTech/LTCI
 * 
 * Authors: see AUTHORS
 * 
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the Eclipse Public License as published by Eclipse,
 * either version 1.0 of the License, or (at your option) any later version.
 * This program 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
 * Eclipse Public License for more details.
 * You should have received a copy of the Eclipse Public License
 * along with this program.  If not, see 
 * http://www.eclipse.org/org/documents/epl-v10.php
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * Compare the given lists of strings. Return {@code true} if they are 
     * equivalents: they contain the same strings in any order.
     * Case is insensitive. {@code false} otherwise.<BR><BR>
     * This method is different from List.equals see {@link List#equals(Object)}.
     * 
     * @param list1 an list of strings
     * @param list2 an other list of strings
     * @return {@code true} if the lists are equivalents, {@code false} otherwise
     */
    public static boolean compareStringList(List<String> list1, List<String> list2) {
        if (list1.size() == list2.size()) {
            ArrayList<String> l1 = new ArrayList<String>(list1);
            ArrayList<String> l2 = new ArrayList<String>(list2);

            Comparator<String> c = new Comparator<String>() {
                public int compare(String o1, String o2) {
                    return o1.compareToIgnoreCase(o2);
                }
            };

            Collections.sort(l1, c);
            Collections.sort(l2, c);

            Iterator<String> it1 = l1.iterator();
            Iterator<String> it2 = l2.iterator();

            String s1, s2;

            while (it1.hasNext()) {
                s1 = it1.next();
                s2 = it2.next();

                if (!s1.equalsIgnoreCase(s2))
                    return false;
            }

            return true;
        } else {
            return false;
        }
    }
}

Related

  1. compareLists(List list1, List list2)
  2. compareLists(Object obj1, Object obj2)
  3. compareLists(String list1[], String list2[])
  4. compareListsAndNull(List arg1, List arg2)
  5. comparer(Comparator comparator, T object, T... withList)
  6. compareStringLists(List elements1, List elements2)
  7. compareSubstring(List pieces, String s)
  8. compareTermList(final List tl0, final List tl1)
  9. compareTo(List list, int i, int j)