Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将枚举值插入HashMap_Java_Enums_Map - Fatal编程技术网

Java 将枚举值插入HashMap

Java 将枚举值插入HashMap,java,enums,map,Java,Enums,Map,我正在做一个程序,其中我需要将枚举值插入哈希映射。我们真的能做到吗?我尝试了很多方法,但都失败了 有人能帮我吗?通过该程序,我需要实现一个HashMap,其中包含4个线程池(其名称充当键),对应于我有一个ThreapoolExcecutor对象 下面是我的代码: public class MyThreadpoolExcecutorPgm { enum ThreadpoolName { DR, PQ, EVENT, M

我正在做一个程序,其中我需要将枚举值插入哈希映射。我们真的能做到吗?我尝试了很多方法,但都失败了

有人能帮我吗?通过该程序,我需要实现一个HashMap,其中包含4个线程池(其名称充当键),对应于我有一个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()


    }
公共类MyThreadpoolExcecutorPgm{
枚举线程池名称
{
博士
PQ,
事件
混杂的;
}
私有静态字符串threadName;
私有静态HashMap threadpoolExecutorHash;
公共MythReadPoolExcecuteorPgm(字符串p_threadName){
threadName=p_threadName;
}
公共静态void fillthreadpoolExecutorHash(){
int poolsize=3;
int maxpoolsize=3;
长keepAliveTime=10;
ThreadPoolExecutor tp=null;
threadpoolExecutorHash=新HashMap();
ThreadpoolName-poolName;
tp=新线程池执行器(池大小、maxpoolsize、keepAliveTime、,
TimeUnit.SECONDS,新的ArrayBlockingQueue(5));
threadpoolExecutorHash.put(poolName,tp);//这里我无法实现currect put()
}

当然,可以将枚举作为映射中的键

出现错误是因为
threadpoolExecutorHash
String
s映射到
ThreadPoolExecutor
s,并且由于尝试将
String
poolName
)作为键插入而失败

threadpoolExecutorHash = new HashMap<String, ThreadPoolExecutor>();
threadpoolExecutorHash=newhashmap();

threadpoolExecutorHash=newhashmap();


如@ TePaTeTyPyff所提到的,甚至有一个特殊的映射实现,适合使用枚举作为键。

< P>您可以考虑使用一个代替<代码> HashMap < /代码>。<代码>枚举使用枚举值时,比<代码> HashMap < /代码>要快得多,空间效率更高,这似乎正是什么?您正在执行此操作。

如果使用字符串作为HashMap的键,则应改为使用Enum类。您的代码应如下所示:

public class MyThreadpoolExcecutorPgm {
enum ThreadpoolName
{
    DR,
    PQ,
    EVENT,
    MISCELLENEOUS;
}
private static String threadName;
private static HashMap<ThreadpoolName, 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<ThreadpoolName, 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()
}
公共类MyThreadpoolExcecutorPgm{
枚举线程池名称
{
博士
PQ,
事件
混杂的;
}
私有静态字符串threadName;
私有静态HashMap threadpoolExecutorHash;
公共MythReadPoolExcecuteorPgm(字符串p_threadName){
threadName=p_threadName;
}
公共静态void fillthreadpoolExecutorHash(){
int poolsize=3;
int maxpoolsize=3;
长keepAliveTime=10;
ThreadPoolExecutor tp=null;
threadpoolExecutorHash=新HashMap();
ThreadpoolName-poolName;
tp=新线程池执行器(池大小、maxpoolsize、keepAliveTime、,
TimeUnit.SECONDS,新的ArrayBlockingQueue(5));
threadpoolExecutorHash.put(poolName,tp);//这里我无法实现currect put()
}

@aioobe-使用
EnumMap
的主要原因是它专门设计用于从枚举类型到其他类型的映射。我可能不应该将效率作为主要原因,而应该将它作为手头工作的更好工具。感谢您指出这一点;您是compl很好。我删除了我的评论,因为我也意识到了你在评论中所说的:)+1。如果你把enum作为映射中的一个键,建议使用EnumMap(),它具有数组的内部表示形式,并且非常有效。
public class MyThreadpoolExcecutorPgm {
enum ThreadpoolName
{
    DR,
    PQ,
    EVENT,
    MISCELLENEOUS;
}
private static String threadName;
private static HashMap<ThreadpoolName, 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<ThreadpoolName, 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()
}