My code looks like this:
List<String> filterList(List<String> list, String regex) {
List<String> result = new ArrayList<String>();
for (String entry : list) {
if (entry.matches(regex)) {
result.add(entry);
}
}
return result;
}
It returns a list that contains only those entries that match the regex
.
I was wondering if there was a built in function for this along the lines of:
List<String> filterList(List<String> list, String regex) {
List<String> result = new ArrayList<String>();
result.addAll(list, regex);
return result;
}
In order to list Files using regular expressions to filter them we have set the example below: Class DirFilter implements the FilenameFilter. It has a Pattern that is initialized by compiling a String regular expression to a Pattern, using compile (String regex) API method of Pattern. It also has a method boolean accept (File dir, String name).
List<String> filterList (List<String> list, String regex) { return list.stream ().filter (s -> s.matches (regex)).collect (Collectors.toList ()); } Alas I am using Java 7. Show activity on this post. Google's Java library (Guava) has an interface Predicate<T> which might be pretty useful for your case.
List<String> filterList (List<String> list, String regex) { List<String> result = new ArrayList<String> (); for (String entry : list) { if (entry.matches (regex)) { result.add (entry); } } return result; } It returns a list that contains only those entries that match the regex .
First the list is converted into a stream. This stream is then filtered with the predicate and the stream is converted back into a list with the collect () method. Thanks to the lambda notation of Java 8, the Predicate can also be passed as a simple function, which shortens the expression even more: makes the filtering even clearer.
In addition to the answer from Konstantin: Java 8 added Predicate
support to the Pattern
class via asPredicate
, which calls Matcher.find()
internally:
Pattern pattern = Pattern.compile("...");
List<String> matching = list.stream()
.filter(pattern.asPredicate())
.collect(Collectors.toList());
Pretty awesome!
In java 8 you can do something like this using new stream API:
List<String> filterList(List<String> list, String regex) {
return list.stream().filter(s -> s.matches(regex)).collect(Collectors.toList());
}
Google's Java library(Guava) has an interface Predicate<T>
which might be pretty useful for your case.
static String regex = "yourRegex";
Predicate<String> matchesWithRegex = new Predicate<String>() {
@Override
public boolean apply(String str) {
return str.matches(regex);
}
};
You define a predicate like the one above and then filter your list based on this predicate with a single-line code:
Iterable<String> iterable = Iterables.filter(originalList, matchesWithRegex);
And to convert the iterable to a list, you can again use Guava:
ArrayList<String> resultList = Lists.newArrayList(iterable);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With