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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:57:01  来源:igfitidea点击:

HashMap for classes, not objects

java

提问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 HashMapbut 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<?>>.

Classhere 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 Mapis 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

也可以看看

Related questions

相关问题



Imposing restrictions with bounded wilcards

使用有界通配符施加限制

Here's an example of imposing bounded wildcards to have a Mapwhose 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.classfrom being used as a key, since Stringdoes 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<?>>();