在Java中,有可能为类的实例创建一个类型安全的Map类。 [英] In Java is it possible to create a type-safe Map of classes to instances of their class?

查看:211
本文介绍了在Java中,有可能为类的实例创建一个类型安全的Map类。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个使用类作为键的映射来返回该类的一个实例。例如:

 < T>地图< Class< T>,T> instanceMap = new HashMap< Class< T>,T> (); 
instanceMap.put(Boolean.class,Boolean.TRUE);
instanceMap.put(String.class,asdf);
instanceMap.put(Integer.class,11);

布尔型b = instanceMap.get(Boolean.class);
Integer i = instanceMap.get(Integer.class);
String s = instanceMap.get(String.class);

这可能吗?我有一种感觉,不是因为我不能指出 T 是为了泛型而不是名为T的类。它感觉有点像高阶泛型。



编辑:我知道我可以尝试扩展Map并实现自己的包装等,但我特意要求使用Java的通用支持来完成此操作。我对这个想法更感兴趣,而不是这个特殊情况。



编辑2:正如下面指出的,这是一个重复的特别是一个子问题): Java地图的值受限于键的类型参数。如果我能够找到像这样雄辩的措辞,我可能会找到答案,而不是公布这个。

我了解你,你说你创建这张地图后,你想用类似的东西填充它。

  f.put(String.class,Hello); 
f.put(Integer.class,new Integer(42));
f.put(x.getClass(),x);

等。对吗?



在这种情况下,我认为答案是否定的,你不能用泛型来做到这一点。泛型说,对于一个给定的类的实例 - 在这种情况下的地图 - 你正在指定适用的类型。因此,如果您说新的HashMap< String,Integer> ;; ,则表示针对此映射的所有操作都将使用一个字符串的值和一个整数。但这不是你想要在这种情况下做的。您希望能够将任何类型的对象放入该类中,然后根据该对象的类型约束该键的可接受类型。这不是泛型的工作原理。它们不是彼此之间的关系,它们是任何给定实例的常量。



当然,您可以创建这样的映射:新的HashMap< Class,Object>; 。这不会强制该类成为相应对象的类,但它可以让您输入这些值。



除此之外,我认为您需要一个包装。我应该指出包装器的put只需要一个参数,因为它可能通过执行 getClass()来确定参数的类别,所以不会有需要告诉它?

I would like to create a map that uses a class as a key to return an instance of that class. Something like:

<T> Map< Class<T>, T > instanceMap = new HashMap< Class<T>, T > ();
instanceMap.put( Boolean.class, Boolean.TRUE );
instanceMap.put( String.class, "asdf" );   
instanceMap.put( Integer.class, 11 );

Boolean b = instanceMap.get( Boolean.class );
Integer i = instanceMap.get( Integer.class );
String s  = instanceMap.get( String.class  );

Is this possible? I have a feeling that no it is not because I cannot indicate that T is meant to be a generic type rather than a class named "T". It is feels somehow like "higher-order generics".

EDIT: I know I could try to extend Map and implement my own wrapper etc, but I am specifically asking about doing this just using using Java's generic support. I am more interested in the idea rather than this particular case.

EDIT 2: As pointed out below, this is a duplicate (or more specifically a subcase) of the question: Java map with values limited by key's type parameter . Had I been able to find wording as eloquent as that, I would have likely found that answer and not posted this.

解决方案

As I understand you, you're saying that after you create this map, you want to populate it with something like ...

f.put(String.class, "Hello");
f.put(Integer.class, new Integer(42));
f.put(x.getClass(), x);

etc. Right?

In that case, I think the answer is no, you cannot do that with generics. Generics say that for a given instance of the class -- the map in this case -- you are specifying the types that are applicable. So if you say new HashMap<String,Integer>;, you are saying that all operations against this map will use a key that is a string and a value that is an integer. But that's not what you want to do in this case. You want to be able to put any sort of object into the class, and then constrain the acceptable types for the key based on the type of the object. That's not how generics work. They're not a relationship between each other, they're a constant for any given instance.

You could, of course, create such a map as new HashMap<Class,Object>;. This wouldn't force the class to be the class of the corresponding object, but it would allow you to enter such values.

Besides that, I think you'd need a wrapper. Should I point out that the wrapper's put would only need one parameter, as it could presumably determine the class of the parameter by doing getClass() on it, there'd be no need to tell it?

这篇关于在Java中,有可能为类的实例创建一个类型安全的Map类。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆