将枚举值插入HashMap

vid*_*hya 5 java enums map

我正在做一个程序,我需要将枚举值插入HashMap.我们真的能这样做吗?我在很多方面尝试过它,但失败了.

谁能帮帮我吗?通过该程序,我需要实现一个包含4个线程池(其名称作为键)的HashMap,对应于我有一个ThreapoolExcecutor对象.

下面给出的是我的代码:

public class MyThreadpoolExcecutorPgm {
    enum ThreadpoolName
    {
        DR,
        PQ,
        EVENT,
        MISCELLENEOUS;
    }
    private static String threadName;
    private static HashMap<String, ThreadPoolExecutor> threadpoolExecutorHash;

    public MyThreadpoolExcecutorPgm(String p_threadName) {
        threadName = p_threadName;

    }

    public static void fillthreadpoolExecutorHash() {
        int poolsize = 3;
        int maxpoolsize = 3;
        long keepAliveTime = 10;
        ThreadPoolExecutor tp = null;
        threadpoolExecutorHash = new HashMap<String, ThreadPoolExecutor>();

        ThreadpoolName poolName ;
        tp = new ThreadPoolExecutor(poolsize, maxpoolsize, keepAliveTime,
                TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5));

        threadpoolExecutorHash.put(poolName,tp);    //Here i am failing to implement currect put()


    }
Run Code Online (Sandbox Code Playgroud)

tem*_*def 8

您可能需要考虑使用EnumMap而不是在HashMap这里. EnumMapHashMap使用枚举值更快,更节省空间,这似乎正是你在这里所做的.