HashMap vs Switch statement performance

后端 未结 4 1742
梦谈多话
梦谈多话 2021-01-30 13:14

A HashMap essentially has O(1) performance while a switch state can have either O(1) or O(log(n)) depending on if the compiler uses a tableswitch or lookup switch.

Under

4条回答
  •  無奈伤痛
    2021-01-30 13:40

    In your case, since you are using an Integer key for your HashMap and a plain 'int' for your switch statement, the best performing implementation will be the switch statement unless the number of passes through this section of code is very high (tens or hundreds of thousands).

提交回复
热议问题