diff --git a/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/compareany/CompareAnyBenchmark.java b/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/compareany/CompareAnyBenchmark.java index 70cd1bb3ff..9a4e5e9431 100644 --- a/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/compareany/CompareAnyBenchmark.java +++ b/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/compareany/CompareAnyBenchmark.java @@ -1,6 +1,7 @@ package com.baeldung.compareany; import java.util.Arrays; +import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -31,12 +32,12 @@ public class CompareAnyBenchmark { @Benchmark public boolean compareWithAnyUsingStringUtils() { - return StringUtils.containsAny(fruit, groupOfFruits); + return StringUtils.equalsAny(fruit, groupOfFruits); } @Benchmark public boolean compareWithAnyCaseInsensitiveUsingStringUtils() { - return StringUtils.containsAnyIgnoreCase(fruit, groupOfFruits); + return StringUtils.equalsAnyIgnoreCase(fruit, groupOfFruits); } @Benchmark @@ -44,6 +45,11 @@ public class CompareAnyBenchmark { return Set.of(groupOfFruits).contains(fruit); } + @Benchmark + public boolean compareWithAnyUsingList() { + return List.of(groupOfFruits).contains(fruit); + } + @Benchmark public boolean compareWithAnyUsingRegularExpression() { return fruit.matches(String.join("|", groupOfFruits)); diff --git a/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/compareany/CompareAnyUnitTest.java b/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/compareany/CompareAnyUnitTest.java index 0995eaf372..818035971d 100644 --- a/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/compareany/CompareAnyUnitTest.java +++ b/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/compareany/CompareAnyUnitTest.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Arrays; +import java.util.List; import java.util.Set; import org.apache.commons.lang3.ArrayUtils; @@ -74,6 +75,15 @@ public class CompareAnyUnitTest { assertFalse(compareWithAnyUsingSet(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); } + @Test + void givenStrings_whenCompareUsingList_thenSuccess() { + String presentString = "Apple"; + String notPresentString = "Avocado"; + + assertTrue(compareWithAnyUsingList(presentString, "Mango", "Papaya", "Pineapple", "Apple")); + assertFalse(compareWithAnyUsingList(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); + } + @Test void givenStrings_whenCompareUsingRegularExpression_thenSuccess() { String presentString = "Apple"; @@ -102,17 +112,21 @@ public class CompareAnyUnitTest { } private boolean compareWithAnyUsingStringUtils(String str, String ... strs) { - return StringUtils.containsAny(str, strs); + return StringUtils.equalsAny(str, strs); } private boolean compareWithAnyCaseInsensitiveUsingStringUtils(String str, String ... strs) { - return StringUtils.containsAnyIgnoreCase(str, strs); + return StringUtils.equalsAnyIgnoreCase(str, strs); } private boolean compareWithAnyUsingSet(String str, String ... strs) { return Set.of(strs).contains(str); } + private boolean compareWithAnyUsingList(String str, String ... strs) { + return List.of(strs).contains(str); + } + private boolean compareWithAnyUsingRegularExpression(String str, String ... strs) { return str.matches(String.join("|", strs)); }