Check if Collection contains - Java java.util

Java examples for java.util:Collection Contain

Description

Check if Collection contains

Demo Code

/*//from www .j  a  v a2  s .  c om
 * Copyright (c) 2002-2004
 * All rights reserved.
 */
//package com.java2s;
import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static void main(String[] argv) throws Exception {
        Collection searchIn = java.util.Arrays.asList("asdf", "java2s.com");
        Object[] find = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        System.out.println(contains(searchIn, find));
    }

    public static boolean contains(Collection searchIn, Object[] find) {
        for (Iterator iterator = searchIn.iterator(); iterator.hasNext();) {
            Object[] objects = (Object[]) iterator.next();
            if (arraysEquals(objects, find)) {
                return true;
            }
        }
        return false;
    }

    private static boolean arraysEquals(Object[] mThis, Object[] mThat) {
        if (mThis.length == mThat.length) {
            for (int i = 0; i < mThis.length; i++) {
                if (!((mThis[i] == null) && (mThat[i] == null))) {
                    if ((mThis[i] == null) || (mThat[i] == null)
                            || (!mThis[i].equals(mThat[i]))) {
                        return false;
                    }
                }
            }
            return true;
        } else {
            return false;
        }
    }
}

Related Tutorials