Description

Checks if an input collection contains a given value.

License

Open Source License

Parameter

Parameter Description
A Key type
collection Input collection
value Value to search

Return

true if value is present in collection , and false otherwise. If collection is empty, it will return false

Declaration

public static <A> boolean contains(Collection<A> collection, A value) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors:/*from   w w w.j  a v  a2 s .  c  o  m*/
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

import java.util.*;

public class Main {
    /**
     * Checks if an input collection contains a given value.
     *
     * @param <A> Key type
     * @param collection Input collection
     * @param value Value to search
     * @return {@code true} if {@code value} is present in {@code collection}, and {@code false} otherwise. If {@code collection} is empty, it will return {@code false}
     */
    public static <A> boolean contains(Collection<A> collection, A value) {
        if (collection.isEmpty())
            return false;

        for (A item : collection)
            if (item == value)
                return true;

        return false;
    }
}

Related

  1. contains(Collection coll, Object o, Comparator c)
  2. contains(Collection collection, Object object)
  3. contains(Collection objects, Object o)
  4. contains(Collection searchIn, Object[] find)
  5. contains(Collection stringCollection, String value)
  6. contains(Collection objectArray, Object element)
  7. contains(Collection container, String strSearch)
  8. contains(Collection mainCollection, String str)
  9. contains(Collection c1, Collection c2)

  10. HOME | Copyright © www.java2s.com 2016