Java 如何从实例中获取供应商?
我使用java8中的流API,当我将一个对象映射到另一个对象并将该对象传递给一个需要供应商的方法时,我遇到了一个编译错误。如何将此对象传递给方法 为了更好地解释,我编写了以下代码:Java 如何从实例中获取供应商?,java,java-stream,Java,Java Stream,我使用java8中的流API,当我将一个对象映射到另一个对象并将该对象传递给一个需要供应商的方法时,我遇到了一个编译错误。如何将此对象传递给方法 为了更好地解释,我编写了以下代码: public class SimpleTest { public static class B{ public static B mapFrom(A a){ return new B(); //transform to b } } public
public class SimpleTest {
public static class B{
public static B mapFrom(A a){
return new B(); //transform to b
}
}
public static class A{
}
public static Integer mapToSomethingElseWith(Supplier<B> b){
return 1;
}
public static void example(){
List<A> a = Lists.newArrayList(new A(),new A(),new A());
List<Integer> l = a.stream()
.map(B::mapFrom)
.map(SimpleTest::mapSomethingElseWith); //does not work. Bad return type in method reference: cannot convert java.lang.Integer to R
.collect(Collectors.toList());
}
}
公共类SimpleTest{
公共静态B类{
公共静态B映射自(A){
返回新的B();//转换为B
}
}
公共静态A类{
}
公共静态整数映射到SomethingelsWith(供应商b){
返回1;
}
公共静态void示例(){
List a=Lists.newArrayList(new a(),new a(),new a());
列表l=a.stream()
.map(B::mapFrom)
.map(SimpleTest::mapSomethingElseWith);//不工作。方法引用中的返回类型错误:无法将java.lang.Integer转换为R
.collect(Collectors.toList());
}
}
我当前的(丑陋的)解决方案如下所示:
List<Integer> l = a.stream()
.map(B::mapFrom)
.map((b)-> ((Supplier) () -> b) // map to supplier
.map(SimpleTest::mapSomethingElseWith)
.collect(Collectors.toList());
List l=a.stream()
.map(B::mapFrom)
.map((b)->((供应商)(->b)//映射到供应商
.map(SimpleTest::mapSomethingElseWith)
.collect(Collectors.toList());
存在一些类似但更具表现力的东西?将最后两个
映射组合起来如何:
List<Integer> l = a.stream()
.map(B::mapFrom)
.map(b -> SimpleTest.mapSomethingElse (() -> b))
.collect(Collectors.toList());
List l=a.stream()
.map(B::mapFrom)
.map(b->SimpleTest.mapSomethingElse(()->b))
.collect(Collectors.toList());
将最后两个map
s组合如何:
List<Integer> l = a.stream()
.map(B::mapFrom)
.map(b -> SimpleTest.mapSomethingElse (() -> b))
.collect(Collectors.toList());
List l=a.stream()
.map(B::mapFrom)
.map(b->SimpleTest.mapSomethingElse(()->b))
.collect(Collectors.toList());
当你写:
List<Integer> l = a.stream()
.map(B::mapFrom)
然后,将流
链接到:
.map(SimpleTest::mapSomethingElseWith);
maptosomethingleweith()
定义为maptosomethingleweith(供应商b)
因此,编译器希望有一个maptosomethingleweith()
方法,作为参数Supplier
,而不是B
,但将B
变量传递给
解决问题的一种方法是使用带有显式lambda的map()
()->b
其中b
是lambda的b
类型的参数,lambda是供应商
。
它实际上不接受arg,并返回一个B
实例
你可以这样写:
map(SimpleTest::mapSomethingElseWith);
List<Integer> l = a.stream()
.map(B::mapFrom)
.map(b->SimpleTest.mapToSomethingElseWith(()-> b) )
.collect(Collectors.toList());
map(SimpleTest::mapSomethingElseWith);
列表l=a.stream()
.map(B::mapFrom)
.map(b->SimpleTest.maptosomethingleweith(()->b))
.collect(Collectors.toList());
当你写:
List<Integer> l = a.stream()
.map(B::mapFrom)
然后,将流
链接到:
.map(SimpleTest::mapSomethingElseWith);
maptosomethingleweith()
定义为maptosomethingleweith(供应商b)
因此,编译器希望有一个maptosomethingleweith()
方法,作为参数Supplier
,而不是B
,但将B
变量传递给
解决问题的一种方法是使用带有显式lambda的map()
()->b
其中b
是lambda的b
类型的参数,lambda是供应商
。
它实际上不接受arg,并返回一个B
实例
你可以这样写:
map(SimpleTest::mapSomethingElseWith);
List<Integer> l = a.stream()
.map(B::mapFrom)
.map(b->SimpleTest.mapToSomethingElseWith(()-> b) )
.collect(Collectors.toList());
map(SimpleTest::mapSomethingElseWith);
列表l=a.stream()
.map(B::mapFrom)
.map(b->SimpleTest.maptosomethingleweith(()->b))
.collect(Collectors.toList());
不.map(b->()->b)
工作吗?不,我尝试过此操作,但发生了以下错误:lambda转换的目标类型必须是一个接口调用maptosomethingleweith()
但它被声明为maptosomethingleweith()
sry是一个输入错误不.map(b->()->->b)
work?没有,我尝试过此操作,但发生了以下错误:lambda转换的目标类型必须是一个接口调用MapToSomethingelsWith()
,但它被声明为MapToSomethingelsWith()
sry是一个输入错误