ArrayList filter
In java-8, they introduced the method removeIf
which takes a Predicate
as parameter.
So it will be easy as:
List<String> list = new ArrayList<>(Arrays.asList("How are you",
"How you doing",
"Joe",
"Mike"));
list.removeIf(s -> !s.contains("How"));
Probably the best way is to use Guava
List<String> list = new ArrayList<String>();
list.add("How are you");
list.add("How you doing");
list.add("Joe");
list.add("Mike");
Collection<String> filtered = Collections2.filter(list,
Predicates.containsPattern("How"));
print(filtered);
prints
How are you
How you doing
In case you want to get the filtered collection as a list, you can use this (also from Guava):
List<String> filteredList = Lists.newArrayList(Collections2.filter(
list, Predicates.containsPattern("How")));