Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using conditional operator for long if statement

Tags:

java

I am seeing if there is a better/nicer way of writing a long if statement using multiple || operators.

After looking online I see a lot of examples using conditional binomial operator. Is there a way this can be used when checking if a string equals a word?

The examples online follow this structure:

int price = condition?80:100;

Here is the function:

public boolean validateLetters(String word) {
    if (word.equals("apple") || word.equals("excel") || word.equals("intern")
        || word.equals("orange") || word.equals("car")) {
      return true;
    }
    return false;
  }

The function checks to see the parameter word matches one of the following.

Is there a nicer or more efficient way I could write it?

like image 260
william_ Avatar asked Aug 24 '21 07:08

william_


People also ask

Can we use conditional operator in if statement?

Yes you can. A ternary conditional operator is an expression with type inferred from the type of the final two arguments. And expressions can be used as the conditional in an if statement. Naturally, whether or not it's a good thing to do depends on the context.

How conditional operator is used in place of if else statement?

It can be used as an alternative for if-else condition if the 'if else' has only one statement each. The conditional operator takes an expression and executes the first statement if the expression evaluates to be true, and the second statement if the expression evaluates to be false.

Can we use ternary operator as if else if?

The conditional operator – also known as the ternary operator – is an alternative form of the if/else statement that helps you to write conditional code blocks in a more concise way. First, you need to write a conditional expression that evaluates into either true or false .

Is conditional operator faster than if?

Yes! The second is vastly more readable.


2 Answers

The simplest way for this specific check is to put all relevant words into a Set and use contains:

private static final Set<String> VALID_WORDS = Set.of("apple", "excel", "intern", "orange", "car");

public boolean validateLetters(String word) {
    return VALID_WORDS.contains(word);
}

Generally speaking typing out a list of very similar checks combined in some way is an indication that you should refactor your code in some way.

like image 58
Joachim Sauer Avatar answered Oct 20 '22 08:10

Joachim Sauer


You can use some sort of collection as it will make code more readable:

// Use a HashSet for performance
 Set<String> values = new HashSet<String>(Arrays.asList("apple", "excel", "intern", "orange", "car"));
    
    // In your method:
  return values.contains(word);

A HashSet is used here to give good look-up performance - even very large hash sets are able to execute contains() extremely quickly.

You can also try this:

public boolean validateLetters(String word) {
    switch(word) {
        case "apple":
        case "excel":
        case "intern":
        case "orange":
        case "car":
            return true;
        default:
            return false;
    }

}
like image 3
Pranav Choudhary Avatar answered Oct 20 '22 06:10

Pranav Choudhary