I am very new to processing.org and Java. I am trying to store objects in a HashMap, and then iterate over the values of the HashMap, calling methods on the stored objects. In order to do that, I assume I need to downcast the iterator to the type of my class, but this throws a ClassCastException ("java.util.HashMap$ValueIterator cannot be cast to sketch_oct27a$MyClass"). The following simplified code demonstrates this behavior:
import java.util.*;
void setup() {
HashMap m = new HashMap();
m.put("First", new MyClass());
m.put("Second", new MyClass());
m.put("Third", new MyClass());
Iterator iter = m.values().iterator();
while (iter.hasNext()) {
((MyClass)iter).SaySomething(); // Throws ClassCastException
iter.next();
}
}
class MyClass {
void SaySomething() {
println("Something");
}
}
How do I call the SaySomething() method through the iterator?