0

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

模块有所有的钥匙

UniqueChars 具有所有值。

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

首先,是否可以从 2 个单独的枚举中读取键和值?

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();
    }
}
4

2 回答 2

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]);
    }
}
于 2013-05-20T22:36:58.420 回答
0

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

public String getUniqueString(int charCount, boolean validSpecialChar, UniqueChars module, 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);
    }
于 2013-05-21T20:35:00.070 回答