Returns a List of all entries from input list that pass the match function using Lambda. - Java Lambda Stream

Java examples for Lambda Stream:Predicate

Description

Returns a List of all entries from input list that pass the match function using Lambda.

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.List;

import java.util.function.Predicate;

public class Main {
    /** Returns a List of all entries from input list that pass the match function. 
     *  The result could be an empty List, but not null. This is very similar
     *  to the builtin "filter" method of Stream.
     *//*from  www.ja va2  s.c  o m*/
    public static <T> List<T> allMatches(List<T> candidates,
            Predicate<T> matchFunction) {
        List<T> matches = new ArrayList<>();
        for (T possibleMatch : candidates) {
            if (matchFunction.test(possibleMatch)) {
                matches.add(possibleMatch);
            }
        }
        return (matches);
    }
}

Related Tutorials