Get the App
SLTechnology News&Howtos  ›  Development  › 

How java uses enumerations to optimize if-else

Shulou Source: shulou.com Published: 2022-06-01 04:39:16 11月16日 Update

This article will explain in detail how java uses enumerations to optimize if-else. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Optimize if-else using enumerations

I mentioned earlier that you can use policy enumerations to optimize a large number of if-else. Of course, if you only judge code that gets values under different conditions, you can consider using enumerations directly to optimize. The effect is similar to that of map.

Or use the if-else of the previous judgment course as a case to optimize.

First, define an enumeration in the class:

Public enum dayEnum {Monday ("Today's English class"), Tuesday ("Today's Chinese class"), Wednesday ("Today's math class"), Thursday ("Today's music class"), Sunday ("Today's programming class"); public String value; dayEnum (String value) {this.value=value;}}

After the definition, you can be similar to the previous way of map, directly get the judgment value in the enumeration, and then directly return the obtained value, which is not more elegant.

Public String getDay (String day) {return dayEnum.valueOf (day) .value;} on "java how to use enumerations to optimize if-else" this article shares here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Tags: Articles effects more different good practical elegant code content writing just mass math math classes articles methods conditions cases knowledge strategies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Huawei MySQL Shulou Technology Linux