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 ;-)
I am aware of the class HashMap
but only used it to assign objects to other objects.
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...
String is as a key of the HashMap.
Yes, we can use any object as key in a Map in java but we need to override the equals() and hashCode() methods of that object class. Please refer an example below, in which I am storing an object of Pair class as key in a hashMap with value type as string in map.
If we wish to create a HashMap of our own class, we need to ensure that the hashcode() of the key of HashMap doesn't change as if it happens then it is impossible to get object value of the key from HashMap.
The key in a hashmap can be any datatype, this includes arrays and objects. Meanwhile, objects can only use integers, strings, and symbols as their keys. Hashmaps are organized as linked lists, so the order of its elements is maintained, which allows the hashmap to be iterable.
You want a 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)
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).
Note that Map<Class<?>, Class<?>>
still maps objects, but the type of those objects are now Class<?>
. They are still objects nonetheless.
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>
.
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>, 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
.
It should have been:
HashMap<Class,Class>
(capital C)
or better:
HashMap<Class<?>,Class<?>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With