6

I was wondering if you could use methods such as 'contains()' in the case of a switch case. I am trying to make the following if statements into a switch case:

String sentence;
if(sentence.contains("abcd")){
// do command a
}
else if(sentence.contains("efgh")){
// do command b
}
else if(sentence.contains("ijkl")){
// do command c
}
else{
//do command d
}

Thank you very much for your help.

Flissie
  • 63
  • 1
  • 1
  • 3

4 Answers4

3

actually you can change this if into switch, but its kinda unreadable:

    final String sentence;
    int mask = sentence.contains("abcd") ? 1 : 0;
    mask |= sentence.contains("efgh") ? 2 : 0;
    mask |= sentence.contains("ijkl") ? 4 : 0;
    switch (mask) {
    case 1:
    case 1 | 2:
    case 1 | 4:
    case 1 | 2 | 4:
        // do command a
        break;
    case 2:
    case 2 | 4:
        // do command b
        break;
    case 4:
        // do command c
        break;
    default:
        // do command d
    }
}
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • 1
    Thank you very much, it worked perfectly! But could you please explain how this method works? I would be most grateful. – Flissie Mar 13 '16 at 16:18
  • 1
    @Flissie it uses bit masks to determine what substring was found, so if 1st bit is set - it means abcd is found, but as soon as other substring could be presented too - you need several case statement for any possible bit mask – Iłya Bursov Mar 13 '16 at 23:37
  • @Iłya Bursov Do you know if this produces more or less efficient bytecode than OP's code? I am working on my own program and have a similar question, with the added complication of needing two different **large** `if...else if...else` statements. Both of these statements check for the exact same Strings (relative to my code, not OP's), just with different String conditions (e.g. `sentence1.contains("astring")` and `sentence2.contains("astring")`) – ajent Aug 03 '20 at 02:26
  • @ajent OPs code is more efficient neither this switch-case – Iłya Bursov Aug 03 '20 at 16:36
1

No, because the case constant must be either:

  • A constant expression
  • Or the name of an enumerator of the same type as the switch expression.

A method call is neither of these.

From the Java Language Specification, section 14.11: The switch statement:

Every case label has a case constant, which is either a constant expression or the name of an enum constant.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
0

Yes, you can get an equivalent bit of code to work using the switch statement assuming you are using JDK 7 or higher. JDK 7 introduced the ability to allow String objects as the expression in a switch statement. This generally produces more efficient bytecode compared to a chain of if-then-else statements invoking the equals method.

String pattern;
String sentence;

if (sentence.contains(pattern))
{
    switch (pattern)
    {
        case "abcd":

            // do command a
            break;

        case "efgh":

            // do command b
            break;

        case "ijkl":

            // do command c
            break;

        default:

            // do command d
            break;
    }
}

Do note however that this only works because the contains method expects a String object, and String objects are now valid inside switch statements. You can't generalize this to work with objects of an arbitrary type.

Dyndrilliac
  • 765
  • 9
  • 16
  • 3
    This does not appear to have the same behavior as the OP's code, unless you have a priori knowledge of which pattern matches. – Andy Thomas Mar 11 '16 at 21:33
  • @AndyThomas The OP has a priori knowledge of which pattern matches; he is hardcoding the patterns at compile time in his example and the sentence is clearly supplied from some form of input at runtime. Hence, my answer's snippet recreates the behavior of the OP's code exactly. – Dyndrilliac Mar 11 '16 at 21:50
  • 1
    The OP's code shows advance knowledge of the set of possible matches. It uses this to find the first matching pattern. If the matching pattern were known beforehand, an `if` or `switch` to find it would not be necessary. – Andy Thomas Mar 11 '16 at 21:58
-1

no you cant. case statements can only compare the values of the thing being "switch"ed. Infact, java only 'recently' started supporting switch statements on Strings, since they are objects and not primitive. In general, switch statements will work only on primitives. The only exception to that, as far as im aware, is for Strings

William B.
  • 85
  • 5