Cannot convert void to java.lang.Void

前端 未结 2 535
有刺的猬
有刺的猬 2021-01-01 11:38

I\'m trying to do the following

interface Updater {
    void update(String value);
}

void update(Collection values, Updater updater) {
    upd         


        
相关标签:
2条回答
  • 2021-01-01 12:08

    A Function returns a value, even if it is declared as being of type Void (you will have to return null then. In contrast, a void method really returns nothing, not even null. So you have to insert the return statement:

    void update(Collection<String> values, Updater updater) {
        update(values, s -> { updater.update(); return null; }, 0);
    }
    

    An alternative would be to change the Function<String,Void> to Consumer<String>, then you can use the method reference:

    void update(Collection<String> values, Updater updater) {
        update(values, updater::update, 0);
    }
    void update(Collection<String> values, Consumer<String> fn, int ignored) {
        // some code
    }
    
    0 讨论(0)
  • 2021-01-01 12:16

    A function returns a value. What you are looking for is the java.util.function.Consumer interface. This has an void accept(T) method and doesn't return a value.

    So you method becomes:

    void update(Collection<String> values, Consumer<String> fn, int ignored) {
        // some code
    }
    
    0 讨论(0)
提交回复
热议问题