for Each value Filtered in a Collection - Android java.util

Android examples for java.util:Collection Filter

Description

for Each value Filtered in a Collection

Demo Code


//package com.book2s;

import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;

import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Main {
    public static <T> void forEachFiltered(Collection<T> toVisitFiltered,
            Predicate<T> predicate, Consumer<T> consumer) {
        filter(toVisitFiltered, predicate).forEach(consumer);
    }//from w  w w .j  a v a2s  .  c  om

    public static <T> List<T> filter(Collection<T> toFilter,
            Predicate<T> predicate) {
        return toFilter.stream().filter(predicate)
                .collect(Collectors.toList());
    }
}

Related Tutorials