From 3a8bae0c8ea7a93b11d9f91435a7f0831c341bb3 Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Tue, 5 Dec 2023 23:20:42 +0530 Subject: [PATCH] BAEL-7308: Comparing One String with Multiple Values in One Expression in Java --- .../compareany/CompareAnyBenchmark.java | 92 +++++++++++++++++++ .../compareany/CompareAnyUnitTest.java | 8 +- 2 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/compareany/CompareAnyBenchmark.java rename core-java-modules/core-java-string-operations-7/src/test/java/{ => com/baeldung}/compareany/CompareAnyUnitTest.java (94%) 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 new file mode 100644 index 0000000000..70cd1bb3ff --- /dev/null +++ b/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/compareany/CompareAnyBenchmark.java @@ -0,0 +1,92 @@ +package com.baeldung.compareany; + +import java.util.Arrays; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +@State(Scope.Benchmark) +@BenchmarkMode(Mode.AverageTime) +@Warmup(iterations = 2) +@Measurement(iterations = 5) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Fork(value = 1) +public class CompareAnyBenchmark { + private final String[] groupOfFruits = {"Apple", "Mango", "Dragon Fruit", "Water Melon", "Avocado", "Guava", "Orange"}; + private final String fruit = "Apple"; + + @Benchmark + public boolean compareWithAnyUsingStringUtils() { + return StringUtils.containsAny(fruit, groupOfFruits); + } + + @Benchmark + public boolean compareWithAnyCaseInsensitiveUsingStringUtils() { + return StringUtils.containsAnyIgnoreCase(fruit, groupOfFruits); + } + + @Benchmark + public boolean compareWithAnyUsingSet() { + return Set.of(groupOfFruits).contains(fruit); + } + + @Benchmark + public boolean compareWithAnyUsingRegularExpression() { + return fruit.matches(String.join("|", groupOfFruits)); + } + + @Benchmark + public boolean compareWithAnyCaseInsensitiveUsingRegularExpression() { + return fruit.matches("(?i)" + String.join("|", groupOfFruits)); + } + + @Benchmark + public boolean compareWithAnyUsingStream() { + return Arrays.stream(groupOfFruits).anyMatch(fruit::equals); + } + + @Benchmark + public boolean compareWithAnyCaseInsensitiveUsingStream() { + return Arrays.stream(groupOfFruits).anyMatch(fruit::equalsIgnoreCase); + } + + @Benchmark + public boolean compareWithAnyUsingArrayUtils() { + return ArrayUtils.contains(groupOfFruits, fruit); + } + + @Benchmark + public boolean compareWithAnyWithIf() { + for(String s : groupOfFruits) { + if (fruit.equals(s)) { + return true; + } + } + return false; + } + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder().include(CompareAnyBenchmark.class.getSimpleName()) + .threads(1) + .shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server") + .build(); + new Runner(options).run(); + } + +} diff --git a/core-java-modules/core-java-string-operations-7/src/test/java/compareany/CompareAnyUnitTest.java b/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/compareany/CompareAnyUnitTest.java similarity index 94% rename from core-java-modules/core-java-string-operations-7/src/test/java/compareany/CompareAnyUnitTest.java rename to core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/compareany/CompareAnyUnitTest.java index 2d6af5456a..0995eaf372 100644 --- a/core-java-modules/core-java-string-operations-7/src/test/java/compareany/CompareAnyUnitTest.java +++ b/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/compareany/CompareAnyUnitTest.java @@ -1,4 +1,4 @@ -package compareany; +package com.baeldung.compareany; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -17,8 +17,8 @@ public class CompareAnyUnitTest { String presentString = "Apple"; String notPresentString = "Avocado"; - assertTrue(matchAnyWithIf(presentString, "Mango", "Papaya", "Pineapple", "Apple")); - assertFalse(matchAnyWithIf(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); + assertTrue(compareWithAnyWithIf(presentString, "Mango", "Papaya", "Pineapple", "Apple")); + assertFalse(compareWithAnyWithIf(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); } @Test @@ -92,7 +92,7 @@ public class CompareAnyUnitTest { assertFalse(compareWithAnyCaseInsensitiveUsingRegularExpression(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); } - private boolean matchAnyWithIf(String str, String ... strs) { + private boolean compareWithAnyWithIf(String str, String ... strs) { for(String s : strs) { if (str.equals(s)) { return true;