JAVA 8: Building a TriFunction functional interface

Java 8 facilitates several functional interface in java.util.function package. If we observe carefully, there are primarily four kinds of interfaces provided in this package, which are:

  • Supplier
  • Consumer
  • Function
  • Predicate

with the following signatures:

Supplier: () -> T
Consumer: T -> ()
Predicate: T -> boolean
Function: T -> R

In this post, alike java.util.function package, we define a new functional interface, called TriFrunction that accepts three arguments as parameters and returns the result after computation.

@FunctionalInterface
public interface TriFunction<T,U,S, R> {

/**
* Applies this function to the given arguments.
*
* @param t the first function argument
* @param u the second function argument
* @param s the third function argument
* @return the function result
*/
R apply(T t, U u, S s);
}

We can use this functional interface to compute the volume of a rectangular prism, by using following lambda expression.

TriFunction volume = (x,y,z) -> x*y*z

To use this lambda expression in the volume computation, we can simply use the following statement

volume.apply(2.4, 5.3, 10.4)

In this post, we have shown how to create a custom functional interface and use it to write concise lambda expressions using Java 8. It seems quite straightforward. If you have any question/remark, please leave a comment below.

Advertisements

3 thoughts on “JAVA 8: Building a TriFunction functional interface

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s