Java HashMap 用于类,而不是对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3456491/
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
HashMap for classes, not objects
提问by kerrl
I want to assign ui-Classes to a model-class each. By this I want to find the class where to store the date from the user interface. Please don't refer to the design but to my question on a HashMap
's usage ;-)
我想将 ui-Classes 分配给每个模型类。通过这个,我想从用户界面找到存储日期的类。请不要参考设计,而是参考我关于 aHashMap
用法的问题;-)
I am aware of the class HashMap
but only used it to assign objects to other objects.
我知道这个类,HashMap
但只用它来将对象分配给其他对象。
How can I manage to link always two CLASSES with each other?
我怎样才能设法将两个类始终相互链接?
public static final HashMap<class,class> componentMap=new HashMap<class, class>();
componentMap.put(ToolPanel.class, ToolComponent.class);
The code above does not work...
上面的代码不起作用...
回答by Eyal Schneider
It should have been:
本来应该是:
HashMap<Class,Class>
(capital C)
(大写 C)
or better:
或更好:
HashMap<Class<?>,Class<?>>
回答by polygenelubricants
You want a Map<Class<?>, Class<?>>
.
你想要一个Map<Class<?>, Class<?>>
.
Class
here refers to java.lang.Class
, which is a generified type. Unless you have more specific bounds, the unbounded wildcard <?>
can be used (see Effective Java 2nd Edition, Item 23: Don't use raw types in new code)
Class
这里指的java.lang.Class
是泛型类型。除非您有更具体的界限,否则<?>
可以使用无界通配符(参见Effective Java 2nd Edition,Item 23:Don't use raw types in new code)
Note that the interface Map
is used here instead of a specific implementation HashMap
(see Effective Java 2nd Edition, Item 52: Refer to objects by their interfaces).
请注意,interface Map
此处使用的是 而不是特定的实现HashMap
(参见Effective Java 2nd Edition,Item 52:通过其接口引用对象)。
Note that Map<Class<?>, Class<?>>
still maps objects, but the type of those objects are now Class<?>
. They are still objects nonetheless.
请注意,Map<Class<?>, Class<?>>
仍然映射对象,但这些对象的类型现在是Class<?>
. 尽管如此,它们仍然是对象。
See also
也可以看看
- Java Tutorial/Generics(original dead link from Archive.org)
- Angelika Langer's Java Generics FAQ
- JLS 15.8.2 Class Literals
A class literal is an expression consisting of the name of a
class
[...] followed by a.
and the tokenclass
. The type of a class literal,C.class
, whereC
is the name of aclass
[...] isClass<C>
.
- Java 教程/泛型(来自 Archive.org 的原始死链接)
- Angelika Langer 的 Java 泛型常见问题解答
- JLS 15.8.2 类文字
类文字是由
class
[...]的名称后跟 a.
和标记组成的表达式class
。类文字的类型,C.class
,其中[...]C
的名称class
是Class<C>
。
Related questions
相关问题
Imposing restrictions with bounded wilcards
使用有界通配符施加限制
Here's an example of imposing bounded wildcards to have a Map
whose keys must be Class<? extends Number>
, and values can be any Class<?>
.
这是一个强加有界通配符的示例,Map
其键必须是Class<? extends Number>
,值可以是 any Class<?>
。
Map<Class<? extends Number>, Class<?>> map
= new HashMap<Class<? extends Number>, Class<?>>();
map.put(Integer.class, String.class); // OK!
map.put(Long.class, StringBuilder.class); // OK!
map.put(String.class, Boolean.class); // NOT OK!
// Compilation error:
// The method put(Class<? extends Number>, Class<?>)
// in the type Map<Class<? extends Number>,Class<?>>
// is not applicable for the arguments (Class<String>, Class<Boolean>)
As you can see, the generic compile-time typesafety mechanism will prevent String.class
from being used as a key, since String
does not extends Number
.
正如你所看到的,通用的编译时类型安全机制将阻止String.class
被用作一个关键,因为String
没有extends Number
。
See also
也可以看看
回答by Sergii Pozharov
The declaration should be:
声明应为:
public static final HashMap<Class<?>, Class<?>> componentMap = new HashMap<Class<?>, Class<?>>();