Functional Interface That Takes Nothing and Returns Nothing

functional interface that takes nothing and returns nothing

How about Runnable :

@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}

Java 8 functional interface with no arguments and no return value

If I understand correctly you want a functional interface with a method void m(). In which case you can simply use a Runnable.

Java 8 lambda Void argument

The syntax you're after is possible with a little helper function that converts a Runnable into Action<Void, Void> (you can place it in Action for example):

public static Action<Void, Void> action(Runnable runnable) {
return (v) -> {
runnable.run();
return null;
};
}

// Somewhere else in your code
Action<Void, Void> action = action(() -> System.out.println("foo"));

Which Functional interface in java.util package has a method with NO arguments and void return type?

You're looking for java.lang.Runnable

How to use functional interface returning void?

As the error explains Void is a class which is not equivalent to void. Supplier<Void> expects to return Void like Supplier<String> will expect String object to return.

So your functional interface should be like below.

It has a void apply() which matches the signature of () -> ...

@FunctionalInterface
public interface ActionIfNotNull {
void apply();
}

However when you search for an inbuild functional interface, you can come up with Runnable as Jon Skeet suggested.

Solution

public static <T> void processIfNull(T o, Runnable s) { // instead of you Runnable can use your own functional interface like ActionIfNotNull
if (Objects.isNull(o)) {
s.run();
}
}

is the type of System.out.println functional interface?

This part of code:

Consumer<Integer> consumer=System.out::println;

is equal to this lambda expression:

Consumer<Integer> consumer=i->System.out.println(i);

and above code block could be written in this way as an anonymous inner class using Consumer interface:

Consumer<Integer> consumer=new Consumer<Integer>(){
public void accept(Integer i){
System.out.println(i);
}
}

So "System.out.println" itself it is not a functional interface, but when you use :: you implicitly implement the Consumer interface.

Usage of BiConsumer interface in java 8

Note that a BiConsumer represents an "operation" that takes 2 parameters.

For example, if you have

BiConsumer<String,String> foo = (a, b) -> System.out.println(a + b);

foo represents the operation System.out.println(a + b); - printing the two parameters concatenated together.

"This operation" just means the operation that the BiConsumer instance represents.

So in the example above, "this operation" for foo just means "System.out.println(a + b);".

The documentation is saying that if you call accept, the operation represented by the instance (for foo, it is System.out.println(a + b);) will be performed.



Related Topics



Leave a reply



Submit