Why is in the first case SomeClass only instantiated once, but in the second case n-times, where n is the number of elements in the stream?
List<SomeClass> list = stream.map(new SomeClass()::method)
.collect(Collectors.toList());
List<SomeClass> list = stream.map(a -> {return new SomeClass().method(a);})
.collect(Collectors.toList());
The method "method" in this case return the object itself (= return this).
So in the first case the list contains only one object, but n-times. In the second case the list contains n-different objects.
For reproducing the issue: main:
Arrays.asList(true, false, true, false).stream().map(new SomeClass()::method)
.collect(Collectors.toList());
System.out.println("----------------------");
Arrays.asList(true, false, true, false).stream().map(a -> {return new SomeClass().method(a);})
.collect(Collectors.toList());
and SomeClass.java:
public class SomeClass {
public SomeClass() {
System.out.println(this);
}
public SomeClass method(Boolean b) {
return this;
}
}
Method references only comes in four kinds (See "Kinds of method references" in this page):
There isn't a kind of method reference that "creates a new object every time", whatever that means. new SomeClass()::methodName
is of the second kind. It refers to the method methodName
of one particular object. That object is a newly created SomeClass
object. It doesn't create any more SomeClass
objects when the method is called, because it is a reference to someMethod
of that particular SomeClass
object that you newly created.
The lambda expression, on the other hand, creates a new SomeClass
every time it is called, because new SomeClass()
is inside the { ... }
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With