Skip to content

Commit 1cf2280

Browse files
authored
Adding "Filtering List using regex code" | BAEL-8736 (#18098)
* Adding filtering using regex code * moving files to different package * correcting indents * organizing files * Update README.md * Update README.md * updating message name * updated test class
1 parent ab7dfad commit 1cf2280

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.listregexfilter;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.regex.Matcher;
7+
import java.util.regex.Pattern;
8+
import java.util.stream.Collectors;
9+
10+
public class RegexFilterExample {
11+
12+
public List<String> filterUsingPatternAndPredicate() {
13+
List<String> fruits = List.of("apple", "banana", "cherry", "apricot", "avocado");
14+
15+
Pattern pattern = Pattern.compile("^a.*");
16+
17+
return fruits.stream()
18+
.filter(pattern.asPredicate())
19+
.toList();
20+
}
21+
22+
public List<String> filterUsingStringMatches() {
23+
List<String> list = List.of("123", "abc", "456def", "789", "xyz");
24+
25+
return list.stream()
26+
.filter(str -> str.matches("\\d+"))
27+
.toList();
28+
}
29+
30+
public List<String> filterUsingPatternCompile() {
31+
List<String> numbers = List.of("one", "two", "three", "four", "five");
32+
List<String> startWithTList = new ArrayList<>();
33+
34+
Pattern pattern = Pattern.compile("^t.*");
35+
36+
for (String item : numbers) {
37+
Matcher matcher = pattern.matcher(item);
38+
if (matcher.matches())
39+
startWithTList.add(item);
40+
}
41+
42+
return startWithTList;
43+
}
44+
45+
public Map<Boolean, List<String>> filterUsingCollectorsPartitioningBy() {
46+
List<String> fruits = List.of("apple", "banana", "apricot", "berry");
47+
48+
Pattern pattern = Pattern.compile("^a.*");
49+
50+
return fruits.stream()
51+
.collect(Collectors.partitioningBy(pattern.asPredicate()));
52+
}
53+
54+
public static void main(String[] args) {
55+
RegexFilterExample regexFilterExample = new RegexFilterExample();
56+
regexFilterExample.filterUsingPatternAndPredicate();
57+
regexFilterExample.filterUsingStringMatches();
58+
regexFilterExample.filterUsingPatternCompile();
59+
regexFilterExample.filterUsingCollectorsPartitioningBy();
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.listregexfilter;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
9+
class RegexFilterExampleUnitTest {
10+
11+
RegexFilterExample regexFilterExample = new RegexFilterExample();
12+
13+
@Test
14+
void whenfilterUsingPatternAndPredicateCalled_thenReturnElementsStartingWithA() {
15+
List<String> newList = regexFilterExample.filterUsingPatternAndPredicate();
16+
Assertions.assertEquals(List.of("apple", "apricot", "avocado"), newList);
17+
}
18+
19+
@Test
20+
void whenfilterUsingStringMatchesCalled_thenReturnElementsContainingDig() {
21+
List<String> newList = regexFilterExample.filterUsingStringMatches();
22+
Assertions.assertEquals(List.of("123", "789"), newList);
23+
}
24+
25+
@Test
26+
void whenfilterUsingPatternCompileCalled_thenReturnElementsStartingWithT3() {
27+
List<String> newList = regexFilterExample.filterUsingPatternCompile();
28+
Assertions.assertEquals(List.of("two", "three"), newList);
29+
}
30+
31+
@Test
32+
void whenfilterUsingCollectorsPartitioningByCalled_thenReturnElementsStartingWithA4() {
33+
Map<Boolean, List<String>> newList = regexFilterExample.filterUsingCollectorsPartitioningBy();
34+
Assertions.assertEquals(List.of("apple", "apricot"), newList.get(true));
35+
Assertions.assertEquals(List.of("banana", "berry"), newList.get(false));
36+
}
37+
}

0 commit comments

Comments
 (0)