“Good” method to call method on each object using Stream API

后端 未结 3 1384
长情又很酷
长情又很酷 2021-02-12 06:14

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 ->         


        
3条回答
  •  一个人的身影
    2021-02-12 06:48

    Sure, but you must use the correct syntax of method reference, i.e. pass the class to which the close() method belong:

    Arrays.stream(log.getHandlers()).forEach(Handler::close);
    

提交回复
热议问题