Reverse lookup of Java enumerations

Source: Internet
Author: User
Tags constant final

Java enumerations are often used to override constant values, and each enumeration value represents a specific constant.

There is often a need to use constants to enumerations when deserializing, which involves the reverse lookup of enumerations.

1. Reverse lookup enumeration from enum name

This method is the first and easiest to use

static method valueof (String name) that can be used to enumerate

The valueof method is a built-in method and is easy to use, but throws an exception when the enumeration is not found.

Students who are familiar with the exception may know that when an exception is thrown, they need to collect the call stack context information for the virtual machine, which has a greater impact on performance.

When used, it is often necessary to use such a deserialization lookup method

E Find (String name, E DefaultValue) that can return a default value instead of throwing an exception when it is not found

2, reverse lookup enumeration from the descriptive value contained in the enumeration value

For example, this enumeration defines

public enum someenum{
    A ("Ades", 1),
    B ("Bdes", 2),
    unknown ("Unknwondes", 3);
     
    private string des;
    private int order;
     
    Private Someenum (String des, int order) {
        this.des = des;
        This.order = order;
    }

In this case, you can add a HashMap to the enumeration class and initialize it when the class is loaded.

public enum someenum{
    A ("Ades"),
    B ("Bdes"),
    Unknown ("Unknwondes");
     
    private string des;
     
    Private static final Map lookup = new HashMap ();
     
    static {
        for (someenum E:enumset.allof (Someenum.class)) {
            lookup.put (e.des, E);
        }
    }
     
    Private Someenum (String des) {
        this.des = des;
    }
     
    public static Someenum Find (String des, Someenum defaultvalue) {
        Someenum value = Lookup.get (DES);
        if (value = = null) {return
            defaultvalue;
        }
        return value;
    }
}

3. Further, if there are multiple descriptive values in the enumeration, and the description value type is not the same

The initialization code and lookup code need to be written more than once.

 public enum someenum{A ("Ades", 1), B ("Bdes", 2), Unknown ("Unknwondes", 3);
    private string des;
     
    private int order;
     
    Private static final Map lookup = new HashMap ();
     
    Private static final Map Lookup_int = new HashMap ();
            static {for (Someenum E:enumset.allof (Someenum.class)) {lookup.put (e.des, E);
        Lookup_int.put (E.order, E);
        } private Someenum (string des, int order) {this.des = des;
    This.order = order;
        public static Someenum Find (String des, Someenum defaultvalue) {Someenum value = Lookup.get (DES);
        if (value = = null) {return defaultvalue;
    return value; 
        public static someenum Find (int order, someenum defaultvalue) {Someenum value = Lookup_int.get;
        if (value = = null) {return defaultvalue;
    return value; }
}

A lot of code has duplicates,

Enumeration classes need to implement their own initialization of reverse lookup mapping relationships in HashMap.

According to the dry principle, duplicate code places generally have a certain bad taste.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.