这是indexloc提供的服务,不要输入任何密码
Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.baeldung.listregexfilter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class RegexFilterExample {

public List<String> filterUsingPatternAndPredicate() {
List<String> fruits = List.of("apple", "banana", "cherry", "apricot", "avocado");

Pattern pattern = Pattern.compile("^a.*");

return fruits.stream()
.filter(pattern.asPredicate())
.toList();
}

public List<String> filterUsingStringMatches() {
List<String> list = List.of("123", "abc", "456def", "789", "xyz");

return list.stream()
.filter(str -> str.matches("\\d+"))
.toList();
}

public List<String> filterUsingPatternCompile() {
List<String> numbers = List.of("one", "two", "three", "four", "five");
List<String> startWithTList = new ArrayList<>();

Pattern pattern = Pattern.compile("^t.*");

for (String item : numbers) {
Matcher matcher = pattern.matcher(item);
if (matcher.matches())
startWithTList.add(item);
}

return startWithTList;
}

public Map<Boolean, List<String>> filterUsingCollectorsPartitioningBy() {
List<String> fruits = List.of("apple", "banana", "apricot", "berry");

Pattern pattern = Pattern.compile("^a.*");

return fruits.stream()
.collect(Collectors.partitioningBy(pattern.asPredicate()));
}

public static void main(String[] args) {
RegexFilterExample regexFilterExample = new RegexFilterExample();
regexFilterExample.filterUsingPatternAndPredicate();
regexFilterExample.filterUsingStringMatches();
regexFilterExample.filterUsingPatternCompile();
regexFilterExample.filterUsingCollectorsPartitioningBy();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.baeldung.listregexfilter;

import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class RegexFilterExampleUnitTest {

RegexFilterExample regexFilterExample = new RegexFilterExample();

@Test
void whenfilterUsingPatternAndPredicateCalled_thenReturnElementsStartingWithA() {
List<String> newList = regexFilterExample.filterUsingPatternAndPredicate();
Assertions.assertEquals(List.of("apple", "apricot", "avocado"), newList);
}

@Test
void whenfilterUsingStringMatchesCalled_thenReturnElementsContainingDig() {
List<String> newList = regexFilterExample.filterUsingStringMatches();
Assertions.assertEquals(List.of("123", "789"), newList);
}

@Test
void whenfilterUsingPatternCompileCalled_thenReturnElementsStartingWithT3() {
List<String> newList = regexFilterExample.filterUsingPatternCompile();
Assertions.assertEquals(List.of("two", "three"), newList);
}

@Test
void whenfilterUsingCollectorsPartitioningByCalled_thenReturnElementsStartingWithA4() {
Map<Boolean, List<String>> newList = regexFilterExample.filterUsingCollectorsPartitioningBy();
Assertions.assertEquals(List.of("apple", "apricot"), newList.get(true));
Assertions.assertEquals(List.of("banana", "berry"), newList.get(false));
}
}