Checks if the given element is contained in the given Iterable - Java java.util

Java examples for java.util:Iterable Element

Description

Checks if the given element is contained in the given Iterable

Demo Code

/*******************************************************************************
 * Copyright (c) 2014 Karlsruhe Institute of Technology, Germany
 *                    Technical University Darmstadt, Germany
 *                    Chalmers University of Technology, Sweden
 * All rights reserved. This program and the accompanying materials
 * are 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
 *
 * Contributors:/* ww w.  j  a v a 2 s  .c  om*/
 *    Technical University Darmstadt - initial API and implementation and/or initial documentation
 *******************************************************************************/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;

public class Main{
    /**
     * Checks if the given element is contained in the given {@link Iterable}.
     * @param iterable The given {@link Iterable} to search in.
     * @param element The element to search.
     * @return {@code true} = contained, {@code false} = not contained
     */
    public static <T> boolean contains(Iterable<T> iterable, T element) {
        boolean found = false;
        if (iterable != null) {
            Iterator<T> iter = iterable.iterator();
            if (iter != null) {
                while (!found && iter.hasNext()) {
                    found = ObjectUtil.equals(iter.next(), element);
                }
            }
        }
        return found;
    }
}

Related Tutorials