Is it possible to run a method, in a consumer, like a method reference, but on the object passed to the consumer:
Arrays.stream(log.getHandlers()).forEach(h -> h.close());
would be a thing like:
Arrays.stream(log.getHandlers()).forEach(this::close);
but that's not working...
Is there a possibility with method references, or is x -> x.method()
the only way working here?
You don't need
this
.YourClassName::close
will call theclose
method on the object passed to the consumer :There are four kinds of method references (Source):
In your case, you need the third kind.
Sure, but you must use the correct syntax of method reference, i.e. pass the class to which the
close()
method belong:I suppose it should be:
Provided the
log.getHandlers()
returns an array of objects of typeHandler
.