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
How to Disable 'X-Frame-Options' Response Header in Spring Security
How to Set an Environment Variable at Runtime from Java
How to Read Excel Cell Having Date with Apache Poi
Any Simple Way to Explain Why I Cannot Do List<Animal> Animals = New Arraylist<Dog>()
Generating 8-Character Only Uuids
Javac Error: Class Names Are Only Accepted If Annotation Processing Is Explicitly Requested
How to Pass a Value from One Jsp to Another Jsp Page
How to View Jre's Source Code in Eclipse
How to Specify the Default Jvm Arguments for Programs I Run from Eclipse
How to Check a Timeperiod Is Overlapping Another Time Period in Java
Why Can't You Reduce the Visibility of a Method in a Java Subclass
Inetaddress.Getlocalhost() Slow to Run (30+ Seconds)
Scanner Class Skips Over Whitespace
Drag and Drop Custom Object from Jlist into Jlabel
Zoom Box for Area Around Mouse Location on Screen
How to Determine If a String Has Non-Alphanumeric Characters