Filters the given list by removing all elements which do not pass a condition set - Android java.util

Android examples for java.util:Set

Description

Filters the given list by removing all elements which do not pass a condition set

Demo Code


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Main{
    /**/*from ww w .j a v a 2s  .com*/
     * Filters the given list by removing all elements which do not
     * pass a condition set in the {@link hr.simplesource.toolbelt.collection.Filter}.
     * <br/> <br/>
     * This method is more suited for those situations that have to explicitly remove
     * data by given condition. For situations where you want to keep elements
     * that satisfy the condition, use {@link hr.simplesource.toolbelt.collection.CollectionUtil#stream(java.util.List, Filter)}
     * @param list a list of data which will be filtered (in the same instance)
     * @param filter Filter that should return <b>true</b> for elements which <b>have to be removed.</b>
     *               For example, if a list of positive integer numbers has to filter out all odd numbers,
     *               this filter should return true for each odd number.
     * @param <E> type of element
     */
    public static <E> void filterList(List<E> list, Filter<E> filter) {
        for (int i = 0; i < list.size(); i++) {
            if (filter.onElement(list.get(i))) {
                list.remove(i);
                i--;
            }
        }
    }
}

Related Tutorials