• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

how to improve performance to avoid multiple if condition

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have one sinario where I need to use multiple if conditions (54 times) to avoid what approach I need to use?

e.g

if(state="AB"){
condi1()
condi2()
} if else(state=='AC'){
condi1()
cond3()
}
........

like this 54 times. Is there any mechanisum except switch or if condition I can do it.

Thanks

Vaishali
 
Master Rancher
Posts: 5161
83
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you could build a Map<String,Runnable> that allows you look up what Runnable to run, and run it.


The Runnable could be replaced by something more complex, maybe a Callable if you need to return a result. or your own custom Command type if you want something else. And the Map could be initialized other ways; I just showed a quick-and-dirty way to put things in it. This just gives the basic idea, and can be adapted as needed.
 
Marshal
Posts: 80653
476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use Strings as indices is a switch-case statement. That only works in Java7, however.
 
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Well, you could build a Map<String,Runnable> that allows you look up what Runnable to run, and run it.


The Runnable could be replaced by something more complex, maybe a Callable if you need to return a result. or your own custom Command type if you want something else. And the Map could be initialized other ways; I just showed a quick-and-dirty way to put things in it. This just gives the basic idea, and can be adapted as needed.



I like this! A nice, simple implementation of strategy that's not over-engineered.
 
Mike Simmons
Master Rancher
Posts: 5161
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell: yes, but he did say he didn't want a switch. To be fair, we haven't explored why he didn't want a switch, and the string switch is probably pretty performant (which seems the main motivation). So it's worth mentioning anyway.

Dennis: thanks! I look forward to something even simpler under Java 8 (with assist from Google Guava), maybe:

to do the same thing.

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Dennis: thanks! I look forward to something even simpler under Java 8 (with assist from Google Guava), maybe...


Not sure if it applies in this case, but couldn't something similar to what you suggested (which I also like, BTW) be done with Enums and EnumSets?

It would appear that each of OP's 'letters' translates to a function of some kind so, providing they're all independent, I suspect you could combine them using an EnumSet (or a List if duplicates are allowed).

@Vaishali: Just a small point. What you're doing has nothing really to do with efficiency; it is dealing with redundancy. Mike's suggestion has the great merit of being very flexible, but if these method combinations are not likely to change very often, then a single method with a huge if statement might not be so terrible - and one nice thing about it is that all you need to do is add one final else to display an error message or throw an Exception.

Winston
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I agree with all the comments so far including (although a part of me cringes at the very thought of it) the huge if-else statement. But if you do decide to leave it as a huge if-else statement then do change your String equality test from == to .equals(..)
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:I agree with all the comments so far including (although a part of me cringes at the very thought of it) the huge if-else statement.



But if you do decide to leave it as a huge if-else statement then do change your String equality test from == to .equals(..)


Well spotted. I'd also add: put it in a method.

Winston
 
Mike Simmons
Master Rancher
Posts: 5161
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:
I agree with all the comments so far including (although a part of me cringes at the very thought of it) the huge if-else statement. But if you do decide to leave it as a huge if-else statement then do change your String equality test from == to .equals(..)


Well, the original wouldn't even compile, since it's = not ==. But good point.
 
Vaishali Paramane
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply

If I used Map and Runnable I can't catch return value.

In my code condition1 ... these methods returns true or false but using this solution I can't catch return value.
 
Vaishali Paramane
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got one more solution using enum. Is this good approach or anything else ?



Thanks

Vaishali
 
Mike Simmons
Master Rancher
Posts: 5161
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vaishali Paramane wrote:If I used Map and Runnable I can't catch return value.


Well, as I said above:

Mike Simmons wrote:The Runnable could be replaced by something more complex, maybe a Callable if you need to return a result.


Check out java.util.concurrent.Callable; it does just what you want. Or you can make your own new interface.

Vaishali Paramane wrote:In my code condition1 ... these methods returns true or false but using this solution I can't catch return value.


In the code example you showed, any result from those conditions was being completely ignored, so I continued the practice.

Vaishali Paramane wrote:I got one more solution using enum. Is this good approach or anything else ?


That could be fine, but you appear to have lost the ability to look up by strings such as AB or AC, which was why I suggested a Map. If that lookup is no longer important to you, then you appear to be asking a different question than what you started with.
 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:...he

is a "she" actually!
The idea of Map is awesome and I have seen used in many real life complicated areas. And is better performer when it comes to being compared with switch-case and if-else.
 
Mike Simmons
Master Rancher
Posts: 5161
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajdeep Biswas wrote:

Mike Simmons wrote:...he

is a "she" actually!


Thanks - sorry, VP.

Rajdeep Biswas wrote:The idea of Map is awesome and I have seen used in many real life complicated areas. And is better performer when it comes to being compared with switch-case and if-else.


Are you sure it's better performance-wise than a switch? I haven't tested how well it works for Strings, but I'm pretty sure that a switch for primitives is at least as fast as using a Map. What makes you say the Map is more performant?
 
Vaishali Paramane
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply.

but how I can pass value to the call in Callable interface
 
Sheriff
Posts: 28399
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vaishali Paramane wrote:but how I can pass value to the call in Callable interface



This is just "requirements creep". First you didn't say much about what your called things were doing, so people suggested Runnable as a reasonable starting point. Then you said you wanted to get the return value. Okay, that's Callable. Now you say you want to pass values i.e. you want to pass parameters? Then, like people have been saying all along, write your own interface which looks like Runnable or Callable.
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case the problem statement needs to incorporate so many conditional checks, wouldn't a rules engine help? I personally haven't implemented it myself, but have seen colleagues doing it.
 
Vaishali Paramane
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul

sorry about that. When I posted my question I didn't realize that one.
 
Paul Clapham
Sheriff
Posts: 28399
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if I sounded grouchy, I know your original example wasn't meant to be a formal proposal or anything like that. I can see where your different options might be taking different parameters from different parts of your application, so you'd have to build some kind of structure to encapsulate those things.
reply
    Bookmark Topic Watch Topic
  • New Topic