Java - Filtering List Entries by Regex

前端 未结 3 1753
孤街浪徒
孤街浪徒 2020-12-29 07:42

My code looks like this:

List filterList(List list, String regex) {
  List result = new ArrayList();
         


        
3条回答
  •  生来不讨喜
    2020-12-29 08:20

    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 matching = list.stream()
                                .filter(pattern.asPredicate())
                                .collect(Collectors.toList());
    

    Pretty awesome!

提交回复
热议问题