commit
						50416857b1
					
				| @ -0,0 +1,98 @@ | |||||||
|  | package com.baeldung.compareany; | ||||||
|  | 
 | ||||||
|  | import java.util.Arrays; | ||||||
|  | import java.util.List; | ||||||
|  | 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 compareWithMultipleStringsUsingStringUtils() { | ||||||
|  |         return StringUtils.equalsAny(fruit, groupOfFruits); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     public boolean compareCaseInsensitiveWithMultipleStringsUsingStringUtils() { | ||||||
|  |         return StringUtils.equalsAnyIgnoreCase(fruit, groupOfFruits); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     public boolean compareWithMultipleStringsUsingSet() { | ||||||
|  |         return Set.of(groupOfFruits).contains(fruit); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     public boolean compareWithMultipleStringsUsingList() { | ||||||
|  |         return List.of(groupOfFruits).contains(fruit); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     public boolean compareWithMultipleStringsUsingRegularExpression() { | ||||||
|  |         return fruit.matches(String.join("|", groupOfFruits)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     public boolean compareCaseInsensitiveWithMultipleStringsUsingRegularExpression() { | ||||||
|  |         return fruit.matches("(?i)" + String.join("|", groupOfFruits)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     public boolean compareWithMultipleStringsUsingStream() { | ||||||
|  |         return Arrays.stream(groupOfFruits).anyMatch(fruit::equals); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     public boolean compareCaseInsensitiveWithMultipleStringsUsingStream() { | ||||||
|  |         return Arrays.stream(groupOfFruits).anyMatch(fruit::equalsIgnoreCase); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     public boolean compareWithMultipleStringsUsingArrayUtils() { | ||||||
|  |         return ArrayUtils.contains(groupOfFruits, fruit); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Benchmark | ||||||
|  |     public boolean compareWithMultipleStringsUsingIf() { | ||||||
|  |         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(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,150 @@ | |||||||
|  | package com.baeldung.compareany; | ||||||
|  | 
 | ||||||
|  | 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; | ||||||
|  | import org.apache.commons.lang3.StringUtils; | ||||||
|  | import org.junit.jupiter.api.Test; | ||||||
|  | 
 | ||||||
|  | public class CompareAnyUnitTest { | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void givenStrings_whenCompareWithMultipleStringsUsingIf_thenSuccess() { | ||||||
|  |         String presentString = "Apple"; | ||||||
|  |         String notPresentString = "Avocado"; | ||||||
|  | 
 | ||||||
|  |         assertTrue(compareWithMultipleStringsUsingIf(presentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |         assertFalse(compareWithMultipleStringsUsingIf(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void givenStrings_whenCompareWithMultipleStringsUsingArrayUtils_thenSuccess() { | ||||||
|  |         String presentString = "Apple"; | ||||||
|  |         String notPresentString = "Avocado"; | ||||||
|  | 
 | ||||||
|  |         assertTrue(compareWithMultipleStringsUsingArrayUtils(presentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |         assertFalse(compareWithMultipleStringsUsingArrayUtils(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void givenStrings_whenCompareWithMultipleStringsUsingStringUtils_thenSuccess() { | ||||||
|  |         String presentString = "Apple"; | ||||||
|  |         String notPresentString = "Avocado"; | ||||||
|  | 
 | ||||||
|  |         assertTrue(compareWithMultipleStringsUsingStringUtils(presentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |         assertFalse(compareWithMultipleStringsUsingStringUtils(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void givenStrings_whenCompareCaseInsensitiveWithMultipleStringsUsingStringUtils_thenSuccess() { | ||||||
|  |         String presentString = "APPLE"; | ||||||
|  |         String notPresentString = "AVOCADO"; | ||||||
|  | 
 | ||||||
|  |         assertTrue(compareCaseInsensitiveWithMultipleStringsUsingStringUtils(presentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |         assertFalse(compareCaseInsensitiveWithMultipleStringsUsingStringUtils(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void givenStrings_whenCompareWithMultipleStringsUsingStream_thenSuccess() { | ||||||
|  |         String presentString = "Apple"; | ||||||
|  |         String notPresentString = "Avocado"; | ||||||
|  | 
 | ||||||
|  |         assertTrue(compareWithMultipleStringsUsingStream(presentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |         assertFalse(compareWithMultipleStringsUsingStream(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void givenStrings_whenCompareCaseInsensitiveWithMultipleStringsUsingStream_thenSuccess() { | ||||||
|  |         String presentString = "APPLE"; | ||||||
|  |         String notPresentString = "AVOCADO"; | ||||||
|  | 
 | ||||||
|  |         assertTrue(compareCaseInsensitiveWithMultipleStringsUsingStream(presentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |         assertFalse(compareCaseInsensitiveWithMultipleStringsUsingStream(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void givenStrings_whenCompareWithMultipleStringsUsingSet_thenSuccess() { | ||||||
|  |         String presentString = "Apple"; | ||||||
|  |         String notPresentString = "Avocado"; | ||||||
|  | 
 | ||||||
|  |         assertTrue(compareWithMultipleStringsUsingSet(presentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |         assertFalse(compareWithMultipleStringsUsingSet(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void givenStrings_whenCompareWithMultipleStringsUsingList_thenSuccess() { | ||||||
|  |         String presentString = "Apple"; | ||||||
|  |         String notPresentString = "Avocado"; | ||||||
|  | 
 | ||||||
|  |         assertTrue(compareWithMultipleStringsUsingList(presentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |         assertFalse(compareWithMultipleStringsUsingList(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void givenStrings_whenCompareWithMultipleStringsUsingRegularExpression_thenSuccess() { | ||||||
|  |         String presentString = "Apple"; | ||||||
|  |         String notPresentString = "Avocado"; | ||||||
|  | 
 | ||||||
|  |         assertTrue(compareWithMultipleStringsUsingRegularExpression(presentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |         assertFalse(compareWithMultipleStringsUsingRegularExpression(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void givenStrings_whenCompareCaseInsensitiveWithMultipleStringsUsingRegularExpression_thenSuccess() { | ||||||
|  |         String presentString = "APPLE"; | ||||||
|  |         String notPresentString = "AVOCADO"; | ||||||
|  | 
 | ||||||
|  |         assertTrue(compareCaseInsensitiveWithMultipleStringsUsingRegularExpression(presentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |         assertFalse(compareCaseInsensitiveWithMultipleStringsUsingRegularExpression(notPresentString, "Mango", "Papaya", "Pineapple", "Apple")); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private boolean compareWithMultipleStringsUsingIf(String str, String... strs) { | ||||||
|  |         for (String s : strs) { | ||||||
|  |             if (str.equals(s)) { | ||||||
|  |                 return true; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private boolean compareWithMultipleStringsUsingStringUtils(String str, String... strs) { | ||||||
|  |         return StringUtils.equalsAny(str, strs); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private boolean compareCaseInsensitiveWithMultipleStringsUsingStringUtils(String str, String... strs) { | ||||||
|  |         return StringUtils.equalsAnyIgnoreCase(str, strs); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private boolean compareWithMultipleStringsUsingSet(String str, String... strs) { | ||||||
|  |         return Set.of(strs).contains(str); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private boolean compareWithMultipleStringsUsingList(String str, String... strs) { | ||||||
|  |         return List.of(strs).contains(str); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private boolean compareWithMultipleStringsUsingRegularExpression(String str, String... strs) { | ||||||
|  |         return str.matches(String.join("|", strs)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private boolean compareCaseInsensitiveWithMultipleStringsUsingRegularExpression(String str, String... strs) { | ||||||
|  |         return str.matches("(?i)" + String.join("|", strs)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private boolean compareWithMultipleStringsUsingStream(String str, String... strs) { | ||||||
|  |         return Arrays.stream(strs).anyMatch(str::equals); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private boolean compareCaseInsensitiveWithMultipleStringsUsingStream(String str, String... strs) { | ||||||
|  |         return Arrays.stream(strs).anyMatch(str::equalsIgnoreCase); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private boolean compareWithMultipleStringsUsingArrayUtils(String str, String... strs) { | ||||||
|  |         return ArrayUtils.contains(strs, str); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user