[英]HashMap that can infer key name with name of value's class
Is there something out there, maybe Guava, that could infer the key name of a HashMap from the either the classname or variable name of the key value? 是否有某些东西(也许是Guava)可以从键值的类名或变量名推断HashMap的键名? Like: 喜欢:
public Class MyBean {
//basic properties, getters, setters, etc.
}
MyBean myBean = new MyBean();
Map<String, Object> myHashMap = new HashMap<String, Object>();
myHashMap.put(myBean);
System.out.println(myHashMap.get("myBean"));
It would be impossible to do from the variable name - because by the time the value of the variable has been passed to the put
method, the variable name is effectively lost. 用变量名是不可能的-因为当变量的值传递到put
方法时,变量名实际上已经丢失了。 There can be multiple variables referring to the same object, or no variable: 可以有多个变量引用同一对象,或者没有变量:
myHashMap.put(new SomeOtherBean()); // What key would be used?
It would be easy to do it by class name, but that would have a pretty limited utility, I suspect. 通过类名来完成它很容易,但是我怀疑它的实用程序非常有限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.