Java List Contain containsInAnyOrder(final List test, final List control)

Here you can find the source of containsInAnyOrder(final List test, final List control)

Description

Compares the content of the two list, no matter how they are ordered

License

Open Source License

Parameter

Parameter Description
test the list containing items to check
control the list containing the expected values

Return

true

Declaration

public static <T extends Comparable<? super T>> boolean containsInAnyOrder(final List<T> test,
        final List<T> control) 

Method Source Code

//package com.java2s;
/******************************************************************************* 
 * Copyright (c) 2008 - 2014 Red Hat, Inc. and others. 
 * Distributed under license by Red Hat, Inc. All rights reserved. 
 * This program is made available under the terms of the 
 * Eclipse Public License v1.0 which accompanies this distribution, 
 * and is available at http://www.eclipse.org/legal/epl-v10.html 
 * /*from  w  w  w  .ja v a  2s  .  c  o  m*/
 * Contributors: 
 * Xavier Coulon - Initial API and implementation 
 ******************************************************************************/

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * Compares the content of the two list, no matter how they are ordered
     * @param test the list containing items to check
     * @param control the list containing the expected values
     * @return {@code true}
     */
    public static <T extends Comparable<? super T>> boolean containsInAnyOrder(final List<T> test,
            final List<T> control) {
        if (test == null || control == null) {
            return false;
        }
        if (test.size() != control.size()) {
            return false;
        }
        final List<T> orderedControl = new LinkedList<T>(control);
        Collections.sort(orderedControl);
        final List<T> orderedTest = new LinkedList<T>(test);
        Collections.sort(orderedTest);
        final Iterator<T> testIterator = orderedTest.iterator();
        for (Iterator<T> controlIterator = orderedControl.iterator(); controlIterator.hasNext();) {
            T controlItem = controlIterator.next();
            T testItem = testIterator.next();
            if (testItem == null || !testItem.equals(controlItem)) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. containsIgnoreCase(List list, String strIn)
  2. containsIgnoreCase(List list, String findMe)
  3. containsIgnoreCase(List list, String s)
  4. containsIgnoreCase(List list, String soughtFor)
  5. containsIgnoreCase(String str, String... list)
  6. containsIndex(int index, List objects)
  7. containsInEach(List first, List second)
  8. ContainsInList(String list, String value)
  9. containsInOrder(List a, List b)