find First match in List by Predicate - Android java.util

Android examples for java.util:List Contain

Description

find First match in List by Predicate

Demo Code

/**//from  w w w . j  ava  2s  .c o  m
 * Copyright (c) 2011 Loganalysis team and contributors
 *
 * 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:
 *   Raffael Schmid - initial API and implementation
 */
import java.util.List;
import java.util.function.Predicate;

public class Main {
  public static <T> T findFirst(final List<T> list, final Predicate<T> predicate) {
    T retVal = null;
    for (final T item : list) {
      if (predicate.test(item)) {
        retVal = item;
        break;
      }
    }
    return retVal;
  }
}

Related Tutorials