Hashmap vs Array performance

前端 未结 7 1239
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 18:29

Is it (performance-wise) better to use Arrays or HashMaps when the indexes of the Array are known? Keep in mind that the \'objects array/map\' in the example is just an exam

7条回答
  •  独厮守ぢ
    2020-12-02 19:27

    Please, never, ever use extended if / else if / else if / else if / else if / else if cases like that. The reason I repeated it so many times is just to make you feel like your java interpreter does when it hits code-blocks like that.

    As soon as you have more than one else if, either use a hashmap, or a switch / case (java 7 will let you do it on Strings, and java 6 you have to use an enum). An even better solution for read-only checking is an ImmutableMap from a framework like guava; they have highly optimized reads as they don't allow writes.

提交回复
热议问题