Java examples for java.util:Collection Search
Tries to find an one occurrence of an object in the given collection.
/******************************************************************************* * Copyright (c) 2010 SAP AG.//w ww . j av a 2s . c om * 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: * Emil Simeonov - initial API and implementation. * Dimitar Donchev - initial API and implementation. * Dimitar Tenev - initial API and implementation. * Nevena Manova - initial API and implementation. * Georgi Konstantinov - initial API and implementation. * Jakob Spies - initial API and implementation. *******************************************************************************/ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; public class Main{ /** * Tries to find an one occurrence of an object in the given collection. * @param objects the set to search * @param <T> the object type * @param criterion the search criterion * @return the found object if it was found, nil otherwise * @pre objects != null * @pre criterion != null */ public static <T> List<T> find(final Collection<T> objects, final Condition<? super T> criterion) { Nil.checkNil(objects, "objects"); //$NON-NLS-1$ Nil.checkNil(criterion, "criterion"); //$NON-NLS-1$ List<T> findings = new ArrayList<T>(); for (final T obj : objects) { if (criterion.isSatisfied(obj)) { findings.add(obj); } } return findings; } }