Here you can find the source of filterStringList(final String prefix, final String infix, final String suffix, final List list)
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. |
public static List filterStringList(final String prefix, final String infix, final String suffix, final List list)
//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; } }