开发者

Retrieving objects from a HashMap using java

开发者 https://www.devze.com 2023-02-03 18:28 出处:网络
Suppose开发者_运维百科 that you have: Map<something of enum type, Object>; If you know the enum, how do you gain access to the ObjecT?For enums it works the same way as for maps in general:

Suppose开发者_运维百科 that you have:

Map<something of enum type, Object>;

If you know the enum, how do you gain access to the ObjecT?


For enums it works the same way as for maps in general:

Object value = map.get(enumKey);

Note that with enum keys, it is recommended to use an EnumMap instead of HashMap.


Use get:

Object result = map.get(key);


http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

Enums are objects in java. As such, they can be used as keys in the get method.

0

精彩评论

暂无评论...
验证码 换一张
取 消