Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding "Filtering List using regex code" | BAEL-8736 #18098

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions core-java-modules/core-java-regex-3/java-list-regex-filter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/>
</parent>

<groupId>org.example</groupId>
<artifactId>java-list-regex-filter</artifactId>
<version>1.0-SNAPSHOT</version>

<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.baeldung;

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> method1() {
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> method2() {
List<String> list = List.of("123", "abc", "456def", "789", "xyz");

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

public List<String> method3() {
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>> method4() {
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.method1();
regexFilterExample.method2();
regexFilterExample.method3();
regexFilterExample.method4();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.baeldung;

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 whenMethod1Called_thenReturnElementsStartingWithA() {
List<String> newList = regexFilterExample.method1();
Assertions.assertEquals(List.of("apple", "apricot", "avocado"), newList);
}

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

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

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