Hey guys im stuck on a problem.
Say I have an interface Animal. I then have classes that implement it such as Dog, Cat, Goat. say each of these classes has an update() function that they get from the interface.
I have an arraylist of Animal,that includes all different kinds of the animal classes (dog, cat, goat). If i was given a string that says “Goat” how would i search that arraylist and choose only the Goat update() function, ignoring Dog and Cat…
If you really only have the String “Goat” to go on you could do something like this:
Or if the String has nothing to do with the name of the class, you could map a String to an instance of Class:
In my opinion Google’s Guava is the best choice: