Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the corresponding enumeration by value in Java

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "Java how to achieve through the value of the corresponding enumeration", in the daily operation, I believe that many people in the Java how to achieve through the value of the corresponding enumeration problems have doubts, the editor looked up all kinds of data, sorted out a simple and useful method of operation, hope to answer the "Java how to achieve through the value of the corresponding enumeration" of the doubt! Next, please follow the editor to study!

I. background

Java enumeration is a special class that generally represents a set of constants, such as 4 seasons of a year, 12 months of a year, 7 days of a week, and so on.

Recently, I have docked many other systems and found that the same system has different environments (development, testing, formal environment), and the configuration information of each environment is usually not modified, so it is found that using enumerations as configuration items is relatively simple to use, and different environment configurations only need to define one more enumeration value.

The use of enumerations involves returning the corresponding enumerations through the values passed in.

2. Through a value, the query returns the corresponding enumeration (sample code) 2.1, enumerated class @ Getterpublic enum CustomType {TEST ("test", "test", "111"), DEV (" dev "," development "," 222"); String typeCode; String typeName; String orgId; CustomType (String typeCode, String typeName, String orgId) {this.typeCode = typeCode; this.typeName = typeName; this.orgId = orgId }} 2.2, commonly used enumeration methods Values (), ordinal () and valueOf () methods

The enumeration class defined by enum inherits the java.lang.Enum class by default and implements two interfaces, java.lang.Seriablizable and java.lang.Comparable.

The values (), ordinal (), and valueOf () methods are located in the java.lang.Enum class:

Values () returns all the values in the enumeration class.

The ordinal () method finds the index of each enumeration constant, just like an array index.

The valueOf () method returns an enumeration constant for the specified string value.

Pass in the value query enumeration, that is, through the values () method, return all enumerations, and then traverse all enumerations, as long as the passed parameter values are the same as the values of the current enumeration, return the current enumeration

2.3. Return the corresponding enumeration public CustomType find (String typeCode) {for (CustomType value: CustomType.values ()) {if (typeCode.equals (value.getTypeCode () {return value;}} / / return null or throw an exception based on your business failure. Return null;} public CustomType find (String orgId,String typeCode) {if (orgId = = null | | typeCode = = null) {return null;} for (CustomType value: CustomType.values ()) {if (orgId.equals (value.getOrgId ()) & & typeCode.equals (value.getTypeCode () {return value }} / / you can return null or throw an exception if it is not found by your own business. Return null;} III. Search optimization

Each time the search is traversed by the values () method, the time complexity is O (n), while with the HashMap search, the time complexity is O (1).

Although the number of enumerations is usually not very large, it is also very fast to traverse the lookup through the values () method each time. Using HashMap takes up a little more memory, but considering that memory can be reduced from O (n) to O (1), you can take the time to optimize your code.

Private static Map orgCustomType = new HashMap (); static {for (CustomType value: CustomType.values ()) {orgCustomType.put (value.getOrgId (), value);}} public CustomType find (String orgId) {return orgCustomType.get (orgId);} at this point, the study on "how to implement Java to find the corresponding enumerations through values" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report