Find the first element in the array that matches the predicate. - Java Collection Framework

Java examples for Collection Framework:Array Element

Description

Find the first element in the array that matches the predicate.

Demo Code


import java.util.Arrays;

public class Main{
    /**//from w  ww .  j  av a2s .c  o m
     * Find the first element in the array that matches the predicate.
     * 
     * @param <T>       The type of element to use.
     * @param input     The array of candidates to match. Not null.
     * @param predicate The match condition. Not null.
     * @return The first match or null if there is none.
     */
    public static <T> T findFirstMatch(T[] input, Predicate<T> predicate) {
        assert input != null : "Illegal null value as parameter";
        assert predicate != null : "Illegal null value as parameter";

        for (T element : input) {
            if (predicate.matches(element)) {
                return element;
            }
        }
        return null;
    }
    /**
     * Find the first element in the array that matches the predicate.
     * 
     * This is a two-dimensional version of {@link #findFirstMatch(T[], Predicate)},
     * iteration is right-to-left as usual in Java. 
     * 
     * @param <T>       The type of element to use.
     * @param input     The array of candidates to match. Not null.
     * @param predicate The match condition. Not null.
     * @return The first match or null if there is none.
     */
    public static <T> T findFirstMatch(T[][] input, Predicate<T> predicate) {
        assert input != null : "Illegal null value as parameter";
        assert predicate != null : "Illegal null value as parameter";

        for (T[] row : input) {
            for (T element : row) {
                if (predicate.matches(element)) {
                    return element;
                }
            }
        }
        return null;
    }
}

Related Tutorials