Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashMap for classes, not objects

Tags:

java

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...

like image 757
kerrl Avatar asked Aug 11 '10 08:08

kerrl


People also ask

Which of these is the most properly used class as a key in HashMap?

String is as a key of the HashMap.

Can we use class as key in 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.

Can we create HashMap class?

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.

Is a HashMap an object?

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.


2 Answers

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.

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 token class. The type of a class literal, C.class, where C is the name of a class [...] is Class<C>.

Related questions

  • What is a raw type and why shouldn't we use it?

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>, 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.

See also

  • Java Tutorials/Generics/More Fun with Wildcards
  • Angelika Langer's Java Generics FAQ/What is a bounded wildcard?
like image 115
polygenelubricants Avatar answered Nov 03 '22 00:11

polygenelubricants


It should have been:

HashMap<Class,Class>

(capital C)

or better:

HashMap<Class<?>,Class<?>>
like image 36
Eyal Schneider Avatar answered Nov 02 '22 22:11

Eyal Schneider