In Java, you cannot directly compare one string with multiple values in one expression using the ==
operator. However, you can achieve this by using logical OR (||
) operators or by using the equals()
method. Here are two ways to compare one string with multiple values:
||
) operators:String str = "apple"; if (str.equals("apple") || str.equals("banana") || str.equals("orange")) { // str is equal to "apple", "banana", or "orange" }
equals()
method:String str = "apple"; if ("apple".equals(str) || "banana".equals(str) || "orange".equals(str)) { // str is equal to "apple", "banana", or "orange" }
Both methods achieve the same result. The first method directly calls the equals()
method on each string value, while the second method calls the equals()
method on the constant string values.
Choose the method that you find more readable and maintainable for your specific use case.
"Java compare String with multiple values in one expression"
String input = "value"; if (input.equals("value1") || input.equals("value2") || input.equals("value3")) { // Perform actions if input matches any of the values }
"Java check if String matches any value in array"
String input = "value"; String[] values = {"value1", "value2", "value3"}; if (Arrays.asList(values).contains(input)) { // Perform actions if input matches any of the values }
"Java compare String with multiple values using switch case"
String input = "value"; switch (input) { case "value1": case "value2": case "value3": // Perform actions if input matches any of the values break; default: // Perform actions if input doesn't match any of the values break; }
"Java compare String with multiple values using if-else if"
String input = "value"; if (input.equals("value1")) { // Perform actions if input matches value1 } else if (input.equals("value2")) { // Perform actions if input matches value2 } else if (input.equals("value3")) { // Perform actions if input matches value3 }
"Java match String against multiple values using regex"
String input = "value"; if (input.matches("value1|value2|value3")) { // Perform actions if input matches any of the values }
"Java check if String is in a set of values"
String input = "value"; Set<String> values = new HashSet<>(Arrays.asList("value1", "value2", "value3")); if (values.contains(input)) { // Perform actions if input matches any of the values }
"Java compare String with multiple values using streams"
String input = "value"; List<String> values = Arrays.asList("value1", "value2", "value3"); if (values.stream().anyMatch(input::equals)) { // Perform actions if input matches any of the values }
"Java compare String with multiple values using enum"
enum MyEnum { VALUE1, VALUE2, VALUE3 } String input = "value"; if (EnumSet.allOf(MyEnum.class).stream().anyMatch(e -> e.name().equals(input))) { // Perform actions if input matches any of the enum values }
"Java compare String with multiple values using pattern matching"
String input = "value"; if (switch (input) { case "value1", "value2", "value3" -> true; default -> false; }) { // Perform actions if input matches any of the values }
"Java check if String matches any value using StringUtils"
String input = "value"; String[] values = {"value1", "value2", "value3"}; if (StringUtils.equalsAny(input, values)) { // Perform actions if input matches any of the values }
nsurlprotocol subreport mediarecorder urllib2 xls redis-py virtual-reality realloc upi graphql-java