File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to improve performance to avoid multiple if condition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to improve performance to avoid multiple if condition" Watch "how to improve performance to avoid multiple if condition" New topic
Author

how to improve performance to avoid multiple if condition

Vaishali Paramane
Ranch Hand

Joined: Mar 05, 2007
Posts: 106
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


SCJP, SCJD
Preparing for SCWCD
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38478
    
  23
You can also use Strings as indices is a switch-case statement. That only works in Java7, however.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
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.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7698
    
  20

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2249
    
  47

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

Joined: Mar 17, 2011
Posts: 7698
    
  20

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
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
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

Joined: Mar 05, 2007
Posts: 106
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

Joined: Mar 05, 2007
Posts: 106
I got one more solution using enum. Is this good approach or anything else ?



Thanks

Vaishali
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
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.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

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
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
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

Joined: Mar 05, 2007
Posts: 106
Thanks for reply.

but how I can pass value to the call in Callable interface
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
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

Joined: Mar 05, 2007
Posts: 106
Hi Paul

sorry about that. When I posted my question I didn't realize that one.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
jQuery in Action, 2nd edition
 
subject: how to improve performance to avoid multiple if condition