Java enumeration - find the corresponding enumeration by value

Python WeChat ordering applet course video

https://edu.csdn.net/course/detail/36074

Python practical quantitative trading financial management system

https://edu.csdn.net/course/detail/35475

1. Background

Java enumeration is a special class that generally represents a set of constants, such as the 4 seasons of a year, the 12 months of a year, the 7 days of a week, the directions of southeast, northwest, etc.

In recent work, I have docked with many other systems and found that the same system I docked has different environments (development, testing, formal environment), and the configuration information of each environment is usually not modified, so I found that enumerations are used for configuration. Item is relatively simple to use. Different environment configurations only need to define one more enumeration value.

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

2. Through a value, the query returns the corresponding enumeration (sample code)

2.1. Enumeration class
@Getter
public enum CustomType {
    TEST("test","测试","111"),
    DEV("dev","开发","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 the 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 values ​​in the enumeration class.
  • The ordinal() method can find the index of each enumeration constant, just like an array index.
  • The valueOf() method returns an enumeration constant of the specified string value.

To query an enumeration by passing in values, return all enumerations through the values() method, and then traverse all enumerations. As long as the parameter value passed in is the same as the value of the current enumeration, the current enumeration will be returned;

2.3. Return the corresponding enumeration by passing in one or more values.
    public CustomType find(String typeCode){
        for (CustomType value : CustomType.values()) {
            if(typeCode.equals(value.getTypeCode())){
                return value;
            }
        }
        //根据自身的业务 查不到可以返回null,或者抛出异常。
        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;
            }
        }
        //根据自身的业务 查不到可以返回null,或者抛出异常。
        return null;
    }

3. Search optimization

Each time the values() method is used to traverse the search, the time complexity is O(n), while the time complexity of the HashMap search is O(1).

Although the number of enumerations is usually not very large, each traversal search through the values() method is also very fast. Using HashMap will take up a little more memory, but considering this, the time complexity of memory can be reduced from O(n) to O(1). This is a cost-effective thing, and you can still spend time optimizing the code.

    private static Map<String,CustomType> orgCustomType = new HashMap<>();
    static {
        for (CustomType value : CustomType.values()) {
            orgCustomType.put(value.getOrgId(),value);
        }
    }

    public CustomType find(String orgId){
        return orgCustomType.get(orgId);
    }

Guess you like

Origin blog.csdn.net/pythonxxoo/article/details/122757988