How to automatically convert if-else if statement to switch
Checkout settings
- Minimum number of 'if' conditions branches: 3
- Only suggest on null-safe expressions
I just found out that you have to click on the first if
, wait for the light bulb icon to appear and then choose Replace if with Switch
.
Update: as @christopheml pointed out in comments on another answer, "Replace If with Switch" seems to have disappeared in IntelliJ 2018.1. A bug has been filed here.
There are IntelliJ intentions named "Replace If with Switch" and "Replace Switch with If". Check your preferences to see if those options are enabled. If so, I would expect that you would see a quickfix option (Option+Enter on Mac, Alt+Enter on Windows) whenever IntelliJ can automatically handle the conversion from one to the other.
Android studio (intelliJ idea):
Put your cursor on the switch
/if
statement and:
mac: option + return
win: alt + enter