From f600b79b10a9a52358019e9fef53c06e187a3425 Mon Sep 17 00:00:00 2001 From: programenth <139003443+programenth@users.noreply.github.com> Date: Tue, 17 Dec 2024 09:14:09 +0530 Subject: [PATCH 1/3] Adding filtering using regex code --- .../java-list-regex-filter/pom.xml | 39 ++++++++++++ .../java/com/baeldung/RegexFilterExample.java | 61 +++++++++++++++++++ .../baeldung/RegexFilterExampleUnitTest.java | 37 +++++++++++ 3 files changed, 137 insertions(+) create mode 100644 core-java-modules/java-list-regex-filter/pom.xml create mode 100644 core-java-modules/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java create mode 100644 core-java-modules/java-list-regex-filter/src/test/java/com/baeldung/RegexFilterExampleUnitTest.java diff --git a/core-java-modules/java-list-regex-filter/pom.xml b/core-java-modules/java-list-regex-filter/pom.xml new file mode 100644 index 000000000000..25a6b2dd5d12 --- /dev/null +++ b/core-java-modules/java-list-regex-filter/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.3.4 + + + + org.example + java-list-regex-filter + 1.0-SNAPSHOT + + + + central + Central Repository + https://repo1.maven.org/maven2/ + + + + + + org.junit.jupiter + junit-jupiter + test + + + + + 17 + 17 + UTF-8 + + + \ No newline at end of file diff --git a/core-java-modules/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java b/core-java-modules/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java new file mode 100644 index 000000000000..658b7a1058b8 --- /dev/null +++ b/core-java-modules/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java @@ -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 method1() { + List fruits = List.of("apple", "banana", "cherry", "apricot", "avocado"); + + Pattern pattern = Pattern.compile("^a.*"); + + return fruits.stream() + .filter(pattern.asPredicate()) + .toList(); + } + + public List method2() { + List list = List.of("123", "abc", "456def", "789", "xyz"); + + return list.stream() + .filter(str -> str.matches("\\d+")) + .toList(); + } + + public List method3() { + List numbers = List.of("one", "two", "three", "four", "five"); + List 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> method4() { + List 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(); + } +} diff --git a/core-java-modules/java-list-regex-filter/src/test/java/com/baeldung/RegexFilterExampleUnitTest.java b/core-java-modules/java-list-regex-filter/src/test/java/com/baeldung/RegexFilterExampleUnitTest.java new file mode 100644 index 000000000000..f4c5bcee9b53 --- /dev/null +++ b/core-java-modules/java-list-regex-filter/src/test/java/com/baeldung/RegexFilterExampleUnitTest.java @@ -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 newList = regexFilterExample.method1(); + Assertions.assertEquals(List.of("apple", "apricot", "avocado"), newList); + } + + @Test + void whenMethod2Called_ThenReturnElementsContainingDig() { + List newList = regexFilterExample.method2(); + Assertions.assertEquals(List.of("123", "789"), newList); + } + + @Test + void whenMethod1Called_ThenReturnElementsStartingWithT3() { + List newList = regexFilterExample.method3(); + Assertions.assertEquals(List.of("two", "three"), newList); + } + + @Test + void whenMethod1Called_ThenReturnElementsStartingWithA4() { + Map> newList = regexFilterExample.method4(); + Assertions.assertEquals(List.of("apple", "apricot"), newList.get(true)); + Assertions.assertEquals(List.of("banana", "berry"), newList.get(false)); + } +} From 1bb8c82aecb443f190393a75a0c4e504a2f2aa43 Mon Sep 17 00:00:00 2001 From: programenth <139003443+programenth@users.noreply.github.com> Date: Thu, 19 Dec 2024 14:06:41 +0000 Subject: [PATCH 2/3] moving files to different package --- .../java-list-regex-filter/pom.xml | 0 .../src/main/java/com/baeldung/RegexFilterExample.java | 4 ++-- .../java/com/baeldung/RegexFilterExampleUnitTest.java | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) rename core-java-modules/{ => core-java-regex-3}/java-list-regex-filter/pom.xml (100%) rename core-java-modules/{ => core-java-regex-3}/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java (96%) rename core-java-modules/{ => core-java-regex-3}/java-list-regex-filter/src/test/java/com/baeldung/RegexFilterExampleUnitTest.java (76%) diff --git a/core-java-modules/java-list-regex-filter/pom.xml b/core-java-modules/core-java-regex-3/java-list-regex-filter/pom.xml similarity index 100% rename from core-java-modules/java-list-regex-filter/pom.xml rename to core-java-modules/core-java-regex-3/java-list-regex-filter/pom.xml diff --git a/core-java-modules/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java b/core-java-modules/core-java-regex-3/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java similarity index 96% rename from core-java-modules/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java rename to core-java-modules/core-java-regex-3/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java index 658b7a1058b8..47221d84aa79 100644 --- a/core-java-modules/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java +++ b/core-java-modules/core-java-regex-3/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java @@ -15,8 +15,8 @@ public List method1() { Pattern pattern = Pattern.compile("^a.*"); return fruits.stream() - .filter(pattern.asPredicate()) - .toList(); + .filter(pattern.asPredicate()) + .toList(); } public List method2() { diff --git a/core-java-modules/java-list-regex-filter/src/test/java/com/baeldung/RegexFilterExampleUnitTest.java b/core-java-modules/core-java-regex-3/java-list-regex-filter/src/test/java/com/baeldung/RegexFilterExampleUnitTest.java similarity index 76% rename from core-java-modules/java-list-regex-filter/src/test/java/com/baeldung/RegexFilterExampleUnitTest.java rename to core-java-modules/core-java-regex-3/java-list-regex-filter/src/test/java/com/baeldung/RegexFilterExampleUnitTest.java index f4c5bcee9b53..aac458747869 100644 --- a/core-java-modules/java-list-regex-filter/src/test/java/com/baeldung/RegexFilterExampleUnitTest.java +++ b/core-java-modules/core-java-regex-3/java-list-regex-filter/src/test/java/com/baeldung/RegexFilterExampleUnitTest.java @@ -11,25 +11,25 @@ class RegexFilterExampleUnitTest { RegexFilterExample regexFilterExample = new RegexFilterExample(); @Test - void whenMethod1Called_ThenReturnElementsStartingWithA() { + void whenMethod1Called_thenReturnElementsStartingWithA() { List newList = regexFilterExample.method1(); Assertions.assertEquals(List.of("apple", "apricot", "avocado"), newList); } @Test - void whenMethod2Called_ThenReturnElementsContainingDig() { + void whenMethod2Called_thenReturnElementsContainingDig() { List newList = regexFilterExample.method2(); Assertions.assertEquals(List.of("123", "789"), newList); } @Test - void whenMethod1Called_ThenReturnElementsStartingWithT3() { + void whenMethod1Called_thenReturnElementsStartingWithT3() { List newList = regexFilterExample.method3(); Assertions.assertEquals(List.of("two", "three"), newList); } @Test - void whenMethod1Called_ThenReturnElementsStartingWithA4() { + void whenMethod1Called_thenReturnElementsStartingWithA4() { Map> newList = regexFilterExample.method4(); Assertions.assertEquals(List.of("apple", "apricot"), newList.get(true)); Assertions.assertEquals(List.of("banana", "berry"), newList.get(false)); From fa6cdbc1f46c5cbc584038275819c9ca8849748c Mon Sep 17 00:00:00 2001 From: programenth <139003443+programenth@users.noreply.github.com> Date: Sun, 22 Dec 2024 15:16:54 +0000 Subject: [PATCH 3/3] correcting indents --- .../java-list-regex-filter/pom.xml | 58 +++++++++---------- .../java/com/baeldung/RegexFilterExample.java | 6 +- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/core-java-modules/core-java-regex-3/java-list-regex-filter/pom.xml b/core-java-modules/core-java-regex-3/java-list-regex-filter/pom.xml index 25a6b2dd5d12..fb56c3fedc8c 100644 --- a/core-java-modules/core-java-regex-3/java-list-regex-filter/pom.xml +++ b/core-java-modules/core-java-regex-3/java-list-regex-filter/pom.xml @@ -2,38 +2,38 @@ - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 3.3.4 - - + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.3.4 + + - org.example - java-list-regex-filter - 1.0-SNAPSHOT + org.example + java-list-regex-filter + 1.0-SNAPSHOT - - - central - Central Repository - https://repo1.maven.org/maven2/ - - + + + central + Central Repository + https://repo1.maven.org/maven2/ + + - - - org.junit.jupiter - junit-jupiter - test - - + + + org.junit.jupiter + junit-jupiter + test + + - - 17 - 17 - UTF-8 - + + 17 + 17 + UTF-8 + \ No newline at end of file diff --git a/core-java-modules/core-java-regex-3/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java b/core-java-modules/core-java-regex-3/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java index 47221d84aa79..fbec90326fca 100644 --- a/core-java-modules/core-java-regex-3/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java +++ b/core-java-modules/core-java-regex-3/java-list-regex-filter/src/main/java/com/baeldung/RegexFilterExample.java @@ -34,9 +34,9 @@ public List method3() { Pattern pattern = Pattern.compile("^t.*"); for (String item : numbers) { - Matcher matcher = pattern.matcher(item); - if (matcher.matches()) - startWithTList.add(item); + Matcher matcher = pattern.matcher(item); + if (matcher.matches()) + startWithTList.add(item); } return startWithTList;