Java iterate through a set of classes

java, reflection

Solution

You do it with a standard for-each loop:

for (Class clazz: commandClasses) {
    // operate on clazz
}

This works for any Collection that implements the `Iterator` interface.

Problem

I have the following code: ``` Reflections reflections = new Reflections("com.mypackage.cmds"); Set<Class<? extends Command>> commandClasses = reflections.getSubTypesOf(Command.class); ``` What it does is to store in a Set all classes descendant from Command class that are within the package com.mypackage.cmds. Now what I want to do is to iterate or traverse through that returned set so each of those classes are loaded calling Class.fromName method. How can I achieve this? Thanks a lot in advance,

Original source