在构造函数具有非空参数列表的情况下使用构造函数引用
Use of constructor reference where constructor has a non-empty parameter list
给出..
List<Foo> copy(List<Foo> foos) {
return foos
.stream()
.map(foo -> new Foo(foo))
.collect(Collectors.toList());
}
IntelliJ IDEA 2016.1.1 报告 new Foo(foo)
"can be replaced with method reference".
我知道无参数构造函数的 Foo::new
语法,但不知道如何将 foo
作为参数传递。我肯定在这里遗漏了一些东西。
I'm aware of the Foo::new
syntax for the no-arg constructor
那不是 Foo::new
所做的。 This expression will expand to what is needed in the context it's used.
在这种情况下
List<Foo> copy(List<Foo> foos) {
return foos.stream().map(Foo::new).collect(Collectors.toList());
}
会寻找需要 Foo
参数的构造函数。
给出..
List<Foo> copy(List<Foo> foos) {
return foos
.stream()
.map(foo -> new Foo(foo))
.collect(Collectors.toList());
}
IntelliJ IDEA 2016.1.1 报告 new Foo(foo)
"can be replaced with method reference".
我知道无参数构造函数的 Foo::new
语法,但不知道如何将 foo
作为参数传递。我肯定在这里遗漏了一些东西。
I'm aware of the
Foo::new
syntax for the no-arg constructor
那不是 Foo::new
所做的。 This expression will expand to what is needed in the context it's used.
在这种情况下
List<Foo> copy(List<Foo> foos) {
return foos.stream().map(Foo::new).collect(Collectors.toList());
}
会寻找需要 Foo
参数的构造函数。