diff --git a/core-java-modules/core-java-collections-list-6/pom.xml b/core-java-modules/core-java-collections-list-6/pom.xml new file mode 100644 index 0000000000..9bea6358c4 --- /dev/null +++ b/core-java-modules/core-java-collections-list-6/pom.xml @@ -0,0 +1,15 @@ + + + 4.0.0 + core-java-collections-list-6 + core-java-collections-list-6 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + \ No newline at end of file diff --git a/core-java-modules/core-java-collections-list-6/src/test/java/com/baeldung/lists/StringListCaseInsensitiveContainsUnitTest.java b/core-java-modules/core-java-collections-list-6/src/test/java/com/baeldung/lists/StringListCaseInsensitiveContainsUnitTest.java new file mode 100644 index 0000000000..51fafcca6b --- /dev/null +++ b/core-java-modules/core-java-collections-list-6/src/test/java/com/baeldung/lists/StringListCaseInsensitiveContainsUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.lists; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +public class StringListCaseInsensitiveContainsUnitTest { + private final static List THE_LIST = List.of("Game of Thrones", "Forrest Gump", "American Beauty", "Pretty Woman", "Catch Me If You Can"); + + @Test + void whenUsingContains_thenGetExpectedResult() { + assertFalse(THE_LIST.contains("catch me if you can")); + } + + boolean ignoreCaseContainsForLoop(List list, String value) { + for (String e : list) { + if (value.equalsIgnoreCase(e)) + return true; + } + return false; + } + + @Test + void whenUsingIgnoreCaseContainsForLoop_thenGetExpectedResult() { + assertTrue(ignoreCaseContainsForLoop(THE_LIST, "CATCH me if you CAN")); + assertTrue(ignoreCaseContainsForLoop(THE_LIST, "game of thrones")); + assertFalse(ignoreCaseContainsForLoop(THE_LIST, "The Godfather")); + } + + @Test + void whenUsingIgnoreCaseContainsStream_thenGetExpectedResult() { + assertTrue(THE_LIST.stream() + .anyMatch(e -> e.equalsIgnoreCase("CATCH me if you CAN"))); + + assertTrue(THE_LIST.stream() + .anyMatch("game of thrones"::equalsIgnoreCase)); + + assertFalse(THE_LIST.stream() + .anyMatch("The Godfather"::equalsIgnoreCase)); + } +} \ No newline at end of file diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 34e5204868..d0cce5e66f 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -78,6 +78,7 @@ core-java-collections-list core-java-collections-list-2 core-java-collections-list-3 + core-java-collections-list-6 core-java-collections-maps core-java-collections-maps-2 core-java-collections-maps-3