As I know lambda expression can be replaced by method reference without any issues. My IDEs say the same, but the following example shows the opposite. The method reference clearly returns the same object, where as lambda expression returns new objects each time.
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Instance {
int member;
Instance set(int value){
this.member = value;
return this;
}
@Override
public String toString() {
return member + "";
}
public static void main(String[] args) {
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4);
Stream<Integer> stream2 = Stream.of(1, 2, 3, 4);
List<Instance> collect1 = stream1.map(i -> new Instance().set(i)).collect(Collectors.toList());
List<Instance> collect2 = stream2.map(new Instance()::set).collect(Collectors.toList());
System.out.println(collect1);
System.out.println(collect2);
}
}
Here is my output:
[1, 2, 3, 4]
[4, 4, 4, 4]