简体   繁体   中英

Java reflection with unique id

I am trying to build HashMap that its keys are unique id and the value is name of classes that found by reflection.

My problem is to create those unique ids. How can I be surly that each class have unique id, and it's id will never change to. I mean that if I will create new class it will never change the older class id.

UPDATE
I need that the id will be an integer number. (Sorry that I did not mentioned that :P )

It won't be guaranteed to be completely unique but just using the fully qualified class name should be sufficient. The String name of the fully qualified path, like java.lang.Math would be a good "unqiue" id, and should very rarely have a hash collision.

Example

map.put(MyClass.class.toString(), MyClass.class);

EDIT: if you need it to be an int, you can always use the String hashcode

map.put(MyClass.class.toString().hashCode(), MyClass.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM