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<>();
7
  • 2
    Post your code. Your description is rather confusing. Commented Sep 20, 2015 at 2:48
  • I'm not sure what you mean. Could you elaborate further? Commented Sep 20, 2015 at 2: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. Commented Sep 20, 2015 at 2: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. Commented Sep 20, 2015 at 2:54
  • It will work. You can safely use Classes as keys in a HashMap. Commented Sep 20, 2015 at 2:57

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.