Enum – Common uses in Java including multiple values, and conversions

In this article the following uses of Enum is mentioned:
– Simple enum
– Enums with multiple values
– Convert enums to String, String[], List
– Convert String to enum
– Get enum values
– Overloaded enum constructors

Example 1: Simple enum

This enum contains 3 fil type constants, and a default constuctor (with private access modifier).

public enum SimpleFileType
{
    DOCX,
    PDF,
    XLSX;
}

Example 2: Enum with multiple values

This enum contains 3 file type constants, and each constant has two values (file extension, and mime type) which are passed through the explicit constructor.

These values can be accessed using corresponding get methods (Refer Example 3).

Enum constructor is implicitly private; public or protected access modifiers are not allowed.

public enum FileType
{
     DOCX ( “.docx”, “application/vnd.openxmlformats-officedocument.wordprocessingml.document” ),
    PDF ( “.pdf”, “application/pdf” ),
    XLSX ( “.xlsx”, “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet” );

    private final String fileExtension;
    private final String mimeType;

    FileType( String fileExtension, String mimeType )
    {
        this.fileExtension = fileExtension;
        this.mimeType = mimeType;
    }
    public String getFileExtension()
    {
        return fileExtension;
    }
    public String getMimeType()
    {
        return mimeType;
    }
}

Example 3: Uses of enum

package com.codebonneamie.demo;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
|import java.util.stream.Stream;

class EnumTest
{
    public static void main( String args[] )
    {
        //Get value of an enum constant
        System.out.printf( “Get first value: %s %n”, FileType.PDF.getMimeType() );
        System.out.printf( “Get second value: %s %n”, FileType.DOCX.getFileExtension() );

        //String to enum
        FileType fileType = FileType.valueOf( “PDF” );

        //Enum to String
        String str = FileType.XLSX.toString();
        System.out.printf( “Enum to String: %s %n”, str );

        //Get enum values
        FileType[] fileTypes = FileType.values();

        //Enum to List
        List<String> list = Stream.of( FileType.values() )
                .map( Enum :: name )
                .collect( Collectors.toList() );
        System.out.println( “Enum to List: ” + list );

        //Enum to String[]
        String[] arr = Stream.of( FileType.values() )
                .map( Enum :: name )
                .toArray( String[] :: new );
        System.out.println( “Enum to String[]: ” + Arrays.asList( arr ) );
    }
}

Output

Get first value: application/pdf 
Get second value: .docx 
Enum to String: XLSX 
Enum to List: [DOCX, PDF, XLSX]
Enum to String[]: [DOCX, PDF, XLSX]

Example 4: Overloaded enum constructors

public enum CurrencyType
{
    USA ( “North America” ),
    INDIA ( “South Asia”, “INR” ),
    UK ( “North-West Europe”, “GBP” ),
    ECUADOR( “North-West South America” );

    private final String region;
    private final String currency;

    CurrencyType( String region )
    {
        this.region = region;
        this.currency = “USD”;
    }
    CurrencyType( String region, String currency )
    {
        this.region = region;
        this.currency = currency;
    }
    public String getRegion()
    {
        return region;
    }
    public String getCurrency()
    {
        return currency;
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *