Java examples for java.util:List Search
Returns the position of the first occurrence of an object in the given sequence.
/******************************************************************************* * Copyright (c) 2010 SAP AG./*w ww . j av a 2 s. 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{ /** * Returns the position of the first occurrence of an object in the given sequence. * @param objects the sequence to search * @param <T> the object type * @param criterion the search criterion * @return the position of the first occurrence of the object if it was found, -1 otherwise * @pre objects != null * @pre criterion != null */ public static <T> int indexOf(final List<? extends T> objects, final Condition<T> criterion) { Nil.checkNil(objects, "objects"); //$NON-NLS-1$ Nil.checkNil(criterion, "criterion"); //$NON-NLS-1$ int found = -1; for (int i = 0, n = objects.size(); i < n; ++i) { if (criterion.isSatisfied(objects.get(i))) { found = i; break; } } return found; } }