SCJP, SCJD
Preparing for SCWCD
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.
Mike Simmons wrote:Dennis: thanks! I look forward to something even simpler under Java 8 (with assist from Google Guava), maybe...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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(..)
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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(..)
SCJP, SCJD
Preparing for SCWCD
SCJP, SCJD
Preparing for SCWCD
Vaishali Paramane wrote:If I used Map and Runnable I can't catch return value.
Mike Simmons wrote:The Runnable could be replaced by something more complex, maybe a Callable if you need to return a result.
Vaishali Paramane wrote:In my code condition1 ... these methods returns true or false but using this solution I can't catch return value.
Vaishali Paramane wrote:I got one more solution using enum. Is this good approach or anything else ?
is a "she" actually!Mike Simmons wrote:...he
Rajdeep Biswas wrote:
is a "she" actually!Mike Simmons wrote:...he
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.
SCJP, SCJD
Preparing for SCWCD
Vaishali Paramane wrote:but how I can pass value to the call in Callable interface
SCJP, SCJD
Preparing for SCWCD
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |