Java enum reverse look-up best practice

前端 未结 7 692
感情败类
感情败类 2020-12-04 15:07

I saw it suggested on a blog that the following was a reasonable way to do a \"reverse-lookup\" using the getCode(int) in a Java enum:

public en         


        
7条回答
  •  無奈伤痛
    2020-12-04 15:34

    Both ways are perfectly valid. And they have technically the same Big-Oh running time.

    However, if you save all of the values to a Map first, you save the time it takes to iterate through the set each time you want to do a lookup. So, I think that the static map and initializer are a slightly better way to go.

提交回复
热议问题