相关疑难解决方法(0)

怎么做功能组合?

虽然不耐烦地等待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中实现这一点?

java java-8

33
推荐指数
2
解决办法
1万
查看次数

方法引用 - 无效方法引用 - 不能从静态上下文引用

我有以下一段代码

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可以解释一下为什么会这样吗?

java java-8

14
推荐指数
2
解决办法
2897
查看次数

Java后备模式

我正在尝试找到一种实现依赖于第三方库类的服务的好方法.如果库不可用或无法提供答案,我还有一个'默认'实现用作后备.

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

12
推荐指数
1
解决办法
5128
查看次数

具有多个参数的Java 8的Function.Function

我读了很多关于如何在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)

我几乎可以肯定,你可以定义自己的功能界面(即通常创建一个新的文件)制定f3f4,但有一些方法可以简单地定义呢?

java lambda java-8

7
推荐指数
3
解决办法
4055
查看次数

我们如何在java.util.function.Function lambda中有2个参数?

我们可以像这样创建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 lambda java-8

6
推荐指数
2
解决办法
6203
查看次数

如何在java 8中创建复合lambda表达式

我正在尝试在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)

不幸的是我找不到我必须使用的确切语法.

java lambda java-8

2
推荐指数
1
解决办法
616
查看次数