I have a program where in I have a large number of if elseif statements comparing the strings . This structure keeps on increasing as I add a new functionality.
Note: this if elseif is inside a for loop
Since java does not allow switch on Strings , I thought of using a Map<String,Integer> mapping my string to number and then using switch case.
But before performing this. I thought does switch case really enhance the performance. ( If there is not much of performance difference I would rather keep the code as it is as the entire project structure will change)
I think, that in case of large amount of variants in conditional loop, the switch/case variant is preffered. For example, you have 100 variants to choose from. You are entering your for loop and looking the first if statement is true or false. If it is false, you are looking the second one, and then the third and then... Until the condition is true. In the worst case this will be 100 computations. Now lets see, what we have in case/switch statement. You are entering the for loop, comparing your number with switch condition and just jumping to the piece of code you need. In the worst case this will be 100 times faster, than with if/else construction. In the best case it will be the same.
It might indeed be 100 times faster. But often there isn't any point in focusing on some code which represents a tiny fraction of your application's run time and trying to make it faster. Even if you made it take no time at all that would only improve the run time by a tiny fraction.
Of course we have no idea whether the code you describe is significant or not. I suspect it isn't, but really I know nothing about it. And neither does anybody else reading your post. So nobody could possibly say whether changing it would make any significant difference at all. I would leave it alone until two things happen:
You decide the performance of your application needs to be improved.
You determine, by profiling the appliction, that this piece of code is a significant part of the problem.
I thought of using a Map<String,Integer> mapping my string to number and then using switch case.
This concept could be taken one step further: use instances of Runnable to perform the functionality and create the map as Map<String, Runnable>. Then you'll obtain instance to perform your action right from the map, so you can just execute map.get(code).run(); (well, a test that the code was in the map would actually be needed). If you need to have parameters and/or return value, create your own interface to represent your actions.
I would not advice to do this to save the switch execution time, it would clearly be an overkill, but this concept could be more manageable than a switch, depending on your needs.
And then we all jump out and yell "surprise! we got you this tiny ad!"