Java ListIterator Usage filterStringList(final String prefix, final String infix, final String suffix, final List list)

Here you can find the source of filterStringList(final String prefix, final String infix, final String suffix, final List list)

Description

Filters the given list of strings, taking all elements that start with the given prefix, contain the given infix and end with the given suffix and returning them as a new list.

License

Open Source License

Parameter

Parameter Description
prefix Prefix to look for. If null, this method will not filter by prefixes.
infix Infix to look for. If null, this method will not filter by infixes.
suffix Suffix to look for. If null, this method will not filter by suffixes.
list List that is to be filtered. If empty or null, an empty list will be returned.

Return

filtered List

Declaration

public static List filterStringList(final String prefix, final String infix, final String suffix,
        final List list) 

Method Source Code


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

public class Main {
    /**/*from ww  w . j  a  v a  2s  .c o m*/
     * Filters the given list of strings, taking all elements that start with the given prefix, contain the given infix
     * and end with the given suffix and returning them as a new list. The given list will not be modified. All three
     * search arguments are optional.
     * 
     * @param prefix
     *          Prefix to look for. If null, this method will not filter by prefixes.
     * @param infix
     *          Infix to look for. If null, this method will not filter by infixes.
     * @param suffix
     *          Suffix to look for. If null, this method will not filter by suffixes.
     * @param list
     *          List that is to be filtered. If empty or null, an empty list will be returned.
     * @return filtered List<String>
     */
    public static List filterStringList(final String prefix, final String infix, final String suffix,
            final List list) {
        // straightforward implementation with simple string matching. nothing fancy.
        if ((list == null) || list.isEmpty()) {
            return new ArrayList();
        }
        final List result = new ArrayList(list);
        final ListIterator iter = result.listIterator();

        while (iter.hasNext()) {
            final String rowName = (String) iter.next();

            if (prefix != null) {
                if (!rowName.startsWith(prefix)) {
                    iter.remove();

                    continue;
                }
            }

            if (infix != null) {
                if (!(rowName.indexOf(infix) > -1)) {
                    iter.remove();

                    continue;
                }
            }

            if (suffix != null) {
                if (!rowName.endsWith(suffix)) {
                    iter.remove();

                    continue;
                }
            }
        }
        return result;
    }
}

Related

  1. clearNulls(Collection col)
  2. containsUsingListIteratorNext(List list, Integer value)
  3. createCommaSeparatedListOfFullTypeNames(ListIterator strIt)
  4. differingDigits(final int lhs, final int rhs, final int base)
  5. emptyIterator()
  6. findFirstIterator(List list, int fromIndex, int toIndex, T value, Comparator comparator)
  7. findObject(Object item, ListIterator iter)
  8. findPrevious(ListIterator iter)
  9. getAll_SequentialAccess( List idxs, List values)