0

I am storing Classes (not objects) in a HashMap...

But I don't understand if it will work correctly... because I can't override equals() or hashCode(), right? I mean, the HashMap must compare the 2 Classes, not the 2 Objects...

So, will it work simply putting Classes into a HashMap?

or will it cause problems?

protected HashMap<Class<? extends MyBaseClass>, int> someTable = new HashMap<>();
ycomp
  • 8,316
  • 19
  • 57
  • 95
  • 2
    Post your code. Your description is rather confusing. – PM 77-1 Sep 20 '15 at 02:48
  • I'm not sure what you mean. Could you elaborate further? – Vince Sep 20 '15 at 02:49
  • What have you tried and what was the result? As you did in school... please show your work. :) It's part of the process of getting questions answered on SO. It's helpful to you because it forces you to investigate your own problem and think it through. It also proves to readers that you did your homework and made a reasonable attempt to answer your own question. Thirdly, it helps readers find and diagnose the problem resulting in a better answer for you and less time wasted for us. – JeffC Sep 20 '15 at 02:52
  • I need the HashMap to keep track of a bunch of Classes, which I later use to figure out what objects to create from these Classes. The number of objects of each type of class that will be created dynamically, varies - so that's what the HashMap is for - to track how many I will need of each Class. Also, the Classes can be different but all descend from the same "root" Class. Think of it like the HashMap stores only Cars, but they can be BMW, Ford, Toyota models etc.... and this is a template for production... that we are storing say, 3 BMW, 5 Ford and 10 Toyotas to dynamically create later. – ycomp Sep 20 '15 at 02:54
  • It will work. You can safely use `Class`es as keys in a `HashMap`. – Mick Mnemonic Sep 20 '15 at 02:57
  • great, that's what I wanted to know. Thanks. – ycomp Sep 20 '15 at 02:59
  • One thing to note is that depending on the lifetime of the `Map`, there's a chance for a memory leak due to the way [class loaders are linked to classes](http://stackoverflow.com/questions/2625546/is-using-the-class-instance-as-a-map-key-a-best-practice). Usually, you don't need to worry about this, however. – Mick Mnemonic Sep 20 '15 at 03:02

2 Answers2

1

It will work; you can safely use Class objects as keys in a HashMap. The values returned by the getClass() method for different instances of a specific class will refer to the same Class instance. The equals and hashCode methods of Class are inherited directly from Object (== equality and a native hash code implementation).

One thing to note is that depending on the lifetime of the Map, there's a chance for a memory leak due to the way that classes contain a reference to their class loaders. Usually, you don't need to worry about this, however.

Community
  • 1
  • 1
Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30
  • would it work better to store the name of the class as a String, e.g. `"com.example.MyClass"`? You can obtain the Class object with `Class.forName` – yas Sep 20 '15 at 03:09
  • 1
    @dave - It depends. If you have a complicated scenario with multiple classloaders, class names may not be unique. `Class` objects will work properly as keys in that scenario too. – Stephen C Sep 20 '15 at 03:10
0

You said you did not understand. Because your question is WRONG

I am storing Classes (not objects) in a HashMap...

Classes which you store are definitely Objects. They are instances of java.lang.Class class

yelliver
  • 5,648
  • 5
  • 34
  • 65