Counts the number of elements in the given Iterable which are selected by the given IFilter . - Java java.util

Java examples for java.util:Iterable Element

Description

Counts the number of elements in the given Iterable which are selected by the given IFilter .

Demo Code

/*******************************************************************************
 * Copyright (c) 2014 Karlsruhe Institute of Technology, Germany
 *                    Technical University Darmstadt, Germany
 *                    Chalmers University of Technology, Sweden
 * 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://from  w ww .  ja va 2 s .com
 *    Technical University Darmstadt - initial API and implementation and/or initial documentation
 *******************************************************************************/
import java.util.function.Predicate;

public class Main{
    /**
     * Counts the number of elements in the given {@link Iterable} which
     * are selected by the given {@link IFilter}.
     * @param iterable The elements to count in.
     * @param filter The {@link IFilter} to select elements.
     * @return The number of elements selected by the {@link IFilter} in the given {@link Iterable}.
     */
    public static <T> int count(Iterable<T> iterable, Predicate<T> filter) {
        int count = 0;
        if (iterable != null && filter != null) {
            for (T element : iterable) {
                if (filter.test(element)) {
                    count++;
                }
            }
        }
        return count;
    }
}

Related Tutorials