In my code have a method that is supposed to invoke the method doSomething of an object. Upfront, it is not known if the object's class does have the public method or not. Until now, I used the following code:
try {
  Method method = component.getClass().getMethod("doSomething", Boolean.TYPE);
  method.invoke(component, true);
} catch (final NoSuchMethodException e) {
  // do nothing as for some components the method "doSomething" simply does not exist
} 
I now wonder if I should try to avoid the NoSuchMethodExceptionby checking if the object's class does have the public method doSomething. 
final Method method = Arrays.stream(component.getClass().getMethods())
      .filter(m -> m.getName().equals("doSomething")).findFirst().orElse(null);
if (method != null) {
  method.invoke(component, true);
}
What do you think is better?
 
    