虽然不耐烦地等待Java 8发布,在阅读Brian Goetz的精彩"Lambda状态"文章之后,我注意到功能组合根本没有被覆盖.
根据上面的文章,在Java 8中应该可以:
// having classes Address and Person
public class Address {
private String country;
public String getCountry() {
return country;
}
}
public class Person {
private Address address;
public Address getAddress() {
return address;
}
}
// we should be able to reference their methods like
Function<Person, Address> personToAddress = Person::getAddress;
Function<Address, String> addressToCountry = Address::getCountry;
Run Code Online (Sandbox Code Playgroud)
现在,如果我想将这两个函数组合成一个函数映射Person到country,我怎样才能在Java 8中实现这一点?
我有以下一段代码
StringJoiner joiner = new StringJoiner(", ");
joiner.add("Something");
Function<StringJoiner,Integer> lengthFunc = StringJoiner::length;
Function<CharSequence,StringJoiner> addFunc = StringJoiner::add;
Run Code Online (Sandbox Code Playgroud)
最后一行导致错误
Error:(54, 53) java: invalid method reference
non-static method add(java.lang.CharSequence) cannot be referenced from a static context
Run Code Online (Sandbox Code Playgroud)
我知道这个方法不能以静态方式使用,我应该有类似的东西:
Function<CharSequence,StringJoiner> addFunc = joiner::add;
Run Code Online (Sandbox Code Playgroud)
代替.但是我无法理解为什么第三行,StringJoiner::length;用于java编译完全正确.someboedy可以解释一下为什么会这样吗?
我正在尝试找到一种实现依赖于第三方库类的服务的好方法.如果库不可用或无法提供答案,我还有一个'默认'实现用作后备.
public interface Service {
public Object compute1();
public Object compute2();
}
public class DefaultService implements Service {
@Override
public Object compute1() {
// ...
}
@Override
public Object compute2() {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
该服务的实际实现将是这样的:
public class ServiceImpl implements Service {
Service defaultService = new DefaultService();
ThirdPartyService thirdPartyService = new ThirdPartyService();
@Override
public Object compute1() {
try {
Object obj = thirdPartyService.customCompute1();
return obj != null ? obj : defaultService.compute1();
}
catch (Exception e) {
return defaultService.compute1();
}
} …Run Code Online (Sandbox Code Playgroud) java fallback design-patterns strategy-pattern proxy-pattern
我读了很多关于如何在Java 8中轻松定义lambda的例子.这个lambda总是有一个参数,如f1:
Function<Integer,Integer> f1 = (x) -> Math.pow(x,2);
Run Code Online (Sandbox Code Playgroud)
当然,你可以扩展身体像f2:
Function<Integer,Integer> f2 = (x) -> {if (x < 0) return 0;
else return Math.pow(x,2);};
Run Code Online (Sandbox Code Playgroud)
但我找不到一种方法来定义一个具有可变数量的参数的lambda,如f3:
Function<Integer,Integer,Integer> f3 = (x,y) -> {return x + y};
Run Code Online (Sandbox Code Playgroud)
或没有参数如f4:
Function<Double> f4 = () -> {return Math.random()};
Run Code Online (Sandbox Code Playgroud)
我几乎可以肯定,你可以定义自己的功能界面(即通常创建一个新的文件)制定f3和f4,但有一些方法可以简单地定义呢?
我们可以像这样创建lambda函数:
Function<Integer, String> getLambda = (a) -> new String("given value is "a);
Run Code Online (Sandbox Code Playgroud)
我有一个场景,我需要在参数中取2个值.如何使用Function实现这一目标?
例:
getLamda(10,20); // I know this line will give error. How can I acheive this?
Run Code Online (Sandbox Code Playgroud) 我正在尝试在Java 8中使用lambda表达式.我想写的表达式的类型是:
BiConsumer<String, BiConsumer<Boolean,Integer>> cc2;
Run Code Online (Sandbox Code Playgroud)
我应该能够写一个看起来像这样的表达式:
cc2 = (s,(b,i)) -> { System.out.println(s+b+i); }; // this does not compile
Run Code Online (Sandbox Code Playgroud)
不幸的是我找不到我必须使用的确切语法.