Determines if the given lists contain the same elements. - Java java.util

Java examples for java.util:List Contain

Description

Determines if the given lists contain the same elements.

Demo Code


//package com.java2s;
import java.util.List;

public class Main {
    public static void main(String[] argv) {
        List firstList = java.util.Arrays.asList("asdf", "java2s.com");
        List secondList = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(sameElements(firstList, secondList));
    }//from w  w  w .j av a  2 s .  c  om

    /**
     * Determines if the given lists contain the same elements. We suppose that all the elements of the given lists 
     * are different.
     * 
     * @param    firstList                The first list.
     * @param    secondList                The second list.
     * @return                         True if the given lists contain the same elements, false otherwise.
     */
    public static boolean sameElements(List firstList, List secondList) {

        // The size hould be the same, otherwise stop.
        if (firstList.size() != secondList.size()) {
            return false;
        }

        // Iterate over the elements of the first list.
        for (int index = 0; index < firstList.size(); index++) {
            // Check if the element is also in the second list.
            if (!secondList.contains(firstList.get(index))) {
                return false;
            }
        }

        // They heve the same elements.
        return true;

    }
}

Related Tutorials