使用 Java 8 流时的新对象实例化实例、对象、Java

2023-09-07 01:47:56 作者:今晚不熬夜

除了后者的可读性稍好之外,使用以下构造有什么不同吗?

Is there a differnce in using the following contstructs, other than slightly better readability in the latter?

someList.stream().map(item -> new NewClass(item)).collect(Collectors.toList());

someList.stream().map(NewClass::new).collect(Collectors.toList());

推荐答案

一般没有区别.NewClass::new 产生更少的字节码,因为在 lambda 版本中,自动生成的私有方法由 java 编译器从 lambda 主体创建,而 NewClass:new 直接链接到构造函数方法处理.因此,使用方法引用您的类文件大小可能会稍微小一些.但预计不会有显着的性能差异.

Generally there's no difference. NewClass::new produces less bytecode as in lambda version an auto-generated private method is created by java compiler from the lambda body while NewClass:new directly links to the constructor method handle. So using method references you may have slightly less class file size. No significant performance difference is expected though.

另一个区别是方法解析过程.它不适用于您的特定示例,但可能适用于其他代码.例如,你有两个构造函数:

Another difference is method resolution procedure. It's not applicable in your particular example, but may be applicable in other code. For example, you have two constructors:

public NewClass(String a) {...}
public NewClass(String a, String b) {...}

你有一些接受功能接口的方法:

And you have some method which accepts functional interface:

public myMethod(Function<String, NewClass> fn) {...}

然后你可以用 lambda 或函数式接口调用它:

Then you can call it both with lambda or functional interface:

myMethod(str -> new NewClass(str));
myMethod(NewClass::new);

但假设稍后您添加一个具有相同名称的新方法,如下所示:

But suppose that later you add a new method with the same name like this:

public myMethod(BiFunction<String, String, NewClass> fn) {...}

然后方法引用调用将变得不明确并导致编译错误,因为 NewClass::new 现在匹配两个构造函数,而 lambda 仍然明确.

Then method reference call will become ambiguous and will result in compilation error as NewClass::new now matches to both constructors, while lambda is still unambiguous.