Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Good" method to call method on each object using Stream API

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?

like image 213
Jofkos Avatar asked Dec 17 '14 20:12

Jofkos


People also ask

Which method is used to iterate over each element of the stream?

Java Stream forEach() method is used to iterate over all the elements of the given Stream and to perform an Consumer action on each element of the Stream.

What are methods used from stream API?

Some of the most prominent methods used in these examples are the filter() - which allows elements that match the predicate, count() - which counts the number of items in a stream, map() - which applies a function in each element of Stream for transformation, and collect() - which collects the final result of Stream ...

How can I call a method on each element of a list?

Original pre-Java 8 answer: List<Car> cars = //... Function<Car, String> carsToNames = new Function<Car, String>() { @Override public String apply(Car car) { return car. getName(); } } List<String> names = Lists. transform(cars, carsToNames);

Which method of Java stream API allows us to pick a stream of elements that satisfy a predicate?

The filter() method allows us to pick a stream of elements that satisfy a predicate.


3 Answers

You don't need this. YourClassName::close will call the close method on the object passed to the consumer :

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

There are four kinds of method references (Source):

Kind                                                                         Example
----                                                                         -------
Reference to a static method                                                 ContainingClass::staticMethodName
Reference to an instance method of a particular object                       containingObject::instanceMethodName
Reference to an instance method of an arbitrary object of a particular type  ContainingType::methodName
Reference to a constructor                                                   ClassName::new

In your case, you need the third kind.

like image 159
Eran Avatar answered Oct 08 '22 00:10

Eran


I suppose it should be:

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

Provided the log.getHandlers() returns an array of objects of type Handler.

like image 32
Edwin Dalorzo Avatar answered Oct 07 '22 22:10

Edwin Dalorzo


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);
like image 8
matsev Avatar answered Oct 08 '22 00:10

matsev