0

I have an if statement along the lines of:

if (characterStrings[occupation] == "bar-owner" || characterStrings[occupation] == "barrista" || characterStrings[occupation] == "shop owner")

How can I make this and similar or statements more concise in java?

Thanks very much I havent be able to find a documentation of this anywhere,

user2752347
  • 161
  • 6
  • 16

5 Answers5

3

You could use the following code:

if (Arrays.asList("bar-owner", "barrista", "shop owner").contains(characterStrings[occupation]))

This will check if characterStrings[occupation] is any of bar owner, barrista or shop owner.

Petter
  • 4,053
  • 27
  • 33
2

Use switch -case instead of multiple or(||)

From java7 onwards switch supports strings also

small example

switch(characterStrings[occupation])
{
case "bar-owner": //some codes for bar-owner
break;
case "barrista":// codes for barrista
break;
}
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • Thanks for the suggestion also very helpful although not quite what I was looking for. – user2752347 Feb 05 '14 at 10:39
  • @user2752347 I have given a small example,check this – SpringLearner Feb 05 '14 at 10:44
  • 1
    This answers a totally different question. He's not trying to handle each case separately, rather all together, hence the if statement including all of them. – leigero Feb 05 '14 at 10:47
  • @leigero using multiple or statements decreases efficiency thats why java provides switch – SpringLearner Feb 05 '14 at 10:50
  • He isn't doing if else. If you notice the question is asking about a single if statement with multiple arguments. Suppose he was trying to say "If object is a bar-owner, or a barrista, or a shop owner, then slap them in the face." Your switch case will have 3 "slap them in the face" commands which is hardly more efficient. – leigero Feb 05 '14 at 18:52
  • @leigero I gave a small example,In `switch(characterStrings[occupation]) { case "bar-owner": case "barrista": other case }` OP no need to write mutiple times – SpringLearner Feb 06 '14 at 03:42
1

First, you should NOT compare strings using ==. It is nearly always a bug. For example:

    if ("hello" == new String("hello")) {
        System.out.println("Something impossible just happened!!");
    }

(The only cases where it is not a bug involve comparison of String literals and/or manually "interned" String objects. And even then, it is a rather dubious optimization because its correctness depends on you never using "normal" strings.)


In Java 6 and earlier there is no way do a sequence of String equals comparisons that is BOTH more concise than AND as efficient as the original version.

Using Arrays.asList, as in

  if (Arrays.asList("bar-owner", "barrista", 
                    "shop owner").contains(characterStrings[occupation])) {
      // statements
  }

is more concise, but it is also significantly less efficient:

  • The contains call must internally iterate over the elements of the list object, testing each one with equals.

  • The asList call involves allocating and initializing the String[] for the varargs argument, and allocating and initializing the List object returned by the call. (You can potentially "hoist" this to improve performance, but that detracts from the conciseness ...)

In Java 7:

    switch (characterStrings[occupation]) {
    case "bar-owner": case "barrista": case "shop owner":
        // statements
        break;
    }

is more concise, and could also be more efficient. It is plausible that the Java compiler(s) could turn that into a lookup in a hidden static HashSet<String> or the equivalent. There is going to be a "break even" point where cost of a sequence of N equals tests is greater than the cost of a hash table lookup.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

If you have to check all those conditions, then you have to check all those conditions. However, a switch block is cleaner with many cases to check for.

Also note that you are comparing strings with ==. Don't. Use the equals() method.

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
  • Thanks, pray tell: What is better about equals? Thanks again. – user2752347 Feb 05 '14 at 10:39
  • Look for the difference between == and equals, http://stackoverflow.com/a/1643079/2968614 – Aditya Feb 05 '14 at 10:45
  • Try this and see what happens: `String foo = "foo"; String foo2 = "fo"; foo2 += "o"; System.out.println(foo); System.out.println(foo2); System.out.println(foo == foo2);` – Martin Dinov Feb 05 '14 at 10:46
0

If you are comparing strings use equals() method instead of ==. you can use switch

Saurabh Sharma
  • 489
  • 5
  • 20