java 以枚举为键的 HashMap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4944089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:43:31  来源:igfitidea点击:

HashMap for enum as key

java

提问by vidhya

I had posted somewhat similar question before also. I got clarification for my doubts as well. But still I need something more. The Hashmap will be initialized with the enum object as the key and a threadpool instance as the value. I am confused as of how to initialize the HashMap for every object been called by some other process ..To make clear : My program, MyThreadpoolExcecutorPgm.java initializes a HashMap My Progran AdditionHandler.java requests a thread from the HashMap by passing ThreadpoolName (enum). I am getting "No thread available from HashMap" message. Please do help me.
Below given is my code:

我之前也发布过类似的问题。我的疑惑也得到了澄清。但我仍然需要更多的东西。Hashmap 将使用枚举对象作为键和线程池实例作为值进行初始化。我对如何为其他进程调用的每个对象初始化 HashMap 感到困惑。 )。我收到“HashMap 中没有可用线程”消息。请帮助我。
下面给出的是我的代码:

 public class MyThreadpoolExcecutorPgm {

    enum ThreadpoolName {
        DR, BR, SV, 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>();
        for (ThreadpoolName poolName : ThreadpoolName.) // failing to implement
        {
            tp = new ThreadPoolExecutor(poolsize, maxpoolsize, keepAliveTime,
                    TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5));
            threadpoolExecutorHash.put(poolName, tp);
        }
    }

    public static ThreadPoolExecutor getThreadpoolExcecutor(
            ThreadpoolName poolName) {
        ThreadPoolExecutor thread = null;
        if (threadpoolExecutorHash != null && poolName != null) {
            thread = threadpoolExecutorHash.get(poolName);
        } else {
            System.out.println("No thread available from HashMap");
        }
        return thread;
    }
}

AdditionHandler.java

加法处理程序

public class AdditionHandler{

    public void handle() {
        AddProcess setObj = new AddProcess(5, 20);
        ThreadPoolExecutor tpe = null;
        ThreadpoolName poolName =ThreadpoolName.DR; //i am using my enum    
        tpe = MyThreadpoolExcecutorPgm.getThreadpoolExcecutor(poolName);
        tpe.execute(setObj);
    }

    public static void main(String[] args) {
        AdditionHandler obj = new AdditionHandler();
        obj.handle();
    }
}

回答by Jon Skeet

I suspect you're just looking for the static values()method which is added to every enum:

我怀疑您只是在寻找values()添加到每个枚举中的静态方法:

for (ThreadpoolName poolName : ThreadpoolName.getValues())

Alternatively, you can use EnumSet.allOf():

或者,您可以使用EnumSet.allOf()

for (ThreadpoolName poolName : EnumSet.allOf(ThreadpoolName.class))

(As Bozho says, EnumMapis a good alternative here. You still need to loop through the enum values.)

(正如 Bozho 所说,EnumMap这里是一个不错的选择。您仍然需要遍历枚举值。)

回答by Bozho

First, you'd better use EnumMap. Then make sure you have filled the map before you invoked the method.

首先,你最好使用EnumMap. 然后确保在调用该方法之前已填充地图。

回答by OrangeDog

You can iterate through enum values by one of (in descending order of preference)

您可以通过其中之一(按偏好的降序)遍历枚举值

for(Enum value : Enum.values())

for(Enum value : EnumSet.allOf(Enum.class))

for(Enum value : Enum.class.getEnumConstants())

But you should also be using an EnumMap.

但是您也应该使用EnumMap.