我可以将枚举键映射到枚举值吗?

时间:2013-05-20 22:01:56

标签: java enum-map

我有2个枚举(模块,UniqueChars)

模块具有所有键

UniqueChars拥有所有值。

如何使用HashMap或EnumMap映射这些?

首先,是否可以阅读按键和按键。两个单独的枚举值?

public enum modules {
    MODULE("MTREE, VTL_POOL, VTL_ACCESS, SNAPSHOT, NFS_EXPORT, CIFS_SHARE, REPORTS, LOCAL_USERS ");

    private String validModules;

    private modules(String validModules) {
        this.validModules= validModules;
    }
}

public enum UniqueChars {
    LOWER_CASE_LETTERS("abcdefghijklmnopqrstuvwxyz"), 
    UPPER_CASE_LETTERS("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 
    NUMBERS("0123456789"), 
    PUNCTUATION(".-_"), 
    MTREE("*/:<>?\\|"), 
    VTL_POOL("*'?:/\\,"), 
    VTL_ACCESS("*'?:/\\,"),
    SNAPSHOT("`~!@#$%^&*()_+-={}|[]\\:\";'<>?,.'"),
    NFS_EXPORT("_-"),
    CIFS_SHARE("\\/:*?\"<>|+[];,="),
    REPORTS("`~!@#$%^&*()_+-={}|[]\\:\";'<>?,.'"),
    LOCAL_USERS("._-");

    private String validChars;

    private UniqueChars(String validChars) {
        this.validChars = validChars;
    }


    public String getValidChars() {
        return validChars;
    }

    public static String getValidCharStrings(UniqueChars... uniqueChars) {
        StringBuilder validCharsSB = new StringBuilder();
        for (UniqueChars chars : uniqueChars) {
            validCharsSB.append(chars.getValidChars());
        }
        return validCharsSB.toString();
    }
}

3 个答案:

答案 0 :(得分:2)

  

如何使用HashMap或EnumMap映射这些?

很简单,您使用EnumMap<Modules, UniqueChars>。键将是来自一个枚举的值,值将是来自另一个的相应值。这种映射很简单,并且可以正常工作。

  

首先,是否可以阅读按键和按键。两个单独的枚举值?

是的,就像你可以拥有一个枚举一样,你可以拥有任意数量的其他人。

显示一些有关Map用法的代码以获取更多帮助。

答案 1 :(得分:2)

是:

import java.util.*;
enum color {
    r,g,b
}
enum letter {
    a,b,c
}
public class So16659089 {
    public static void main(String[] args) {
        System.out.println(map);
        System.out.println(map2);
    }
    static Map<color,letter> map=new EnumMap<color,letter>(color.class);
    static { // one way
        map.put(color.r,letter.a);
        map.put(color.g,letter.b);
        map.put(color.b,letter.c);
    }
    static Map<color,letter> map2=new EnumMap<color,letter>(color.class);
    static letter[] letters=new letter[]{letter.a,letter.b,letter.c};
    static { // another way
        for(int i=0;i<color.values().length;i++)
            map2.put(color.values()[i],letters[i]);
    }
}

答案 2 :(得分:0)

我在API中使用上面发布的代码。我想用HashMap或EnumMap替换switch块。不想在'n'个案例中使用'n'put声明。

public String getUniqueString(int charCount,boolean validSpecialChar,             UniqueChars模块,boolean invalidSpecialChar,String ... specialChars){

    final int STRING_LENGTH = this.getRandomNumber(charCount);
    String finalString = "";
    String validCharStrings = "";

    if(!validSpecialChar){
        validCharStrings = UniqueChars.getValidCharStrings(
                UniqueChars.LOWER_CASE_LETTERS,
                UniqueChars.UPPER_CASE_LETTERS, UniqueChars.NUMBERS);
    } else {
        UniqueChars moduleName = null;
        switch (module) {
        case MTREE:
            moduleName = UniqueChars.MTREE;
            break;
        case VTL_POOL:
            moduleName = UniqueChars.VTL_POOL;
            break;
        case VTL_ACCESS:
            moduleName = UniqueChars.VTL_ACCESS;
            break;
        case SNAPSHOT:
            moduleName = UniqueChars.SNAPSHOT;
            break;
        case NFS_EXPORT:
            moduleName = UniqueChars.NFS_EXPORT;
            break;
        case CIFS_SHARE:
            moduleName = UniqueChars.CIFS_SHARE;
            break;
        case REPORTS:
            moduleName = UniqueChars.REPORTS;
            break;
        case LOCAL_USERS:
            moduleName = UniqueChars.LOCAL_USERS;
            break;
        }

        // If specialChar is true, special characters are included for the
        // specified module
        validCharStrings = UniqueChars.getValidCharStrings(
                UniqueChars.LOWER_CASE_LETTERS,
                UniqueChars.UPPER_CASE_LETTERS, UniqueChars.NUMBERS,
                moduleName);
    }