Java 8 lambda Void argument -


let's have following functional interface in java 8:

interface action<t, u> {    u execute(t t); } 

and cases need action without arguments or return type. write this:

action<void, void> = () -> { system.out.println("do nothing!"); }; 

however, gives me compile error, need write as

action<void, void> = (void v) -> { system.out.println("do nothing!"); return null;}; 

which ugly. there way rid of void type?

the syntax you're after possible little helper function converts runnable action<void, void> (you can place in action example):

public static action<void, void> action(runnable runnable) {     return (v) -> {         runnable.run();         return null;     }; }  // somewhere else in code  action<void, void> action = action(() -> system.out.println("foo")); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -