Determines if ArrayList B is a sub Set of ArrayList A - Java Collection Framework

Java examples for Collection Framework:Array Sub Array

Description

Determines if ArrayList B is a sub Set of ArrayList A

Demo Code


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

public class Main {
    /**/*from   w  ww  .  j a v  a  2  s  .  c o  m*/
     * Determines if setB is a subSet of setA
     * 
     * @param setA - the full set for the comparison
     * @param setB - the sub set to be tested 
     * @return true if setB is a subSet of setA 
     * @throws java.lang.IllegalArgumentException - 
     *                  when one of setA or setB is null or empty
     */
    public static boolean subSet(ArrayList<Integer> setA,
            ArrayList<Integer> setB) {
        if (null == setA || null == setB) {
            throw new IllegalArgumentException(
                    "Arralist arguments cannot be null");
        }
        if (setA.size() == 0 || setB.size() == 0) {
            throw new IllegalArgumentException(
                    "Arraylist arguments cannot be empty");
        }

        boolean isInList = true;
        for (int i = 0; i < setA.size() && isInList; i++) {
            isInList = false;
            for (int j = 0; j < setB.size() && !isInList; j++) {
                isInList = setA.get(i) == setB.get(j);
            }
        }

        return isInList;
    }
}

Related Tutorials