Welcome to my website. I am always posting links to photo albums, art, technology and other creations. Everything that you will see on my numerous personal sites is powered by the formVistaTM Website Management Engine.

icon.linkedin.jpgicon.twitter.jpg

  • Java In-line Initialization of a Map in the Constructor of an Enum Constant
    03/01/2010 8:52PM

    With Java 5.0 enums became much more powerful, click here for an overview.

    I am working on a project that is using an enum to keep track of an attribute of one of my objects and wanted to initialize a HashMap inline in the call to the enum's constructor.? Such was my delight to find out that using a simple initializer block that it was a snap.

    Here is the relevant snippet of code that illustrates the syntax:

    public enum MediaFileType{
        IMAGE( new HashMap<String, String>() {
                {
                    put(".jpg", "image/jpeg");
                    put(".jpeg", "image/jpeg");
                }
            } );

Advanced Search

Categories

Archives