Function interface – A functional interface in the java.util.function package in Java 8

I had previously written about functional interfaces and their usage. If you are exploring the APIs to be part of Java 8 and especially those APIs which support lambda expressions you will find few interfaces like- Function, Supplier, Consumer, Predicate and others which are all part of the java.util.function package, being used extensively. These interfaces have one abstract method, which is overridden by the lambda expression defined. In this post I will pick Function interface to explain about it in brief and it is one of the interfaces present in java.util.function package.

Function interface has two methods:
 
 

In this post I would like to write about the apply method, creating APIs which accept these interfaces and parameters and then invoke their corresponding methods. We will also look at how the caller of the API can pass in a lambda expression in place of an implementation of the interface. Apart from passing a lambda expression, the users of the API can also pass method references, about which I havent blogged yet.

Function interface is uses in cases where you want to encapsulate some code into a method which accepts some value as an input parameter and then returns another value after performing required operations on the input. The input parameter type and the return type of the method can either be same or different.

Lets look at an API which accepts an implementation of Function interface:

public class FunctionDemo {

   //API which accepts an implementation of 
   //Function interface
  static void modifyTheValue(int valueToBeOperated, 
          Function<Integer, Integer> function){

    int newValue = function.apply(valueToBeOperated);
    /*
     * Do some operations using the new value.
     */
    System.out.println(newValue);
  } 
}

Now lets look at the code which invokes this API:

public static void main(String[] args) {
  int incr = 20;
  int myNumber = 10;
  modifyTheValue(myNumber, val-> val + incr);

  myNumber = 15;
  modifyTheValue(myNumber, val-> val * 10);
  modifyTheValue(myNumber, val-> val - 100);
  modifyTheValue(myNumber, val-> "somestring".length() + val - 100);
}

You can see that the lambda expressions being created accept one parameter and return some value.

I will update soon about the various APIs which use this Function interface as a parameter. Meanwhile the complete code is:

public class FunctionDemo {

  public static void main(String[] args) {
    int incr = 20;
    int myNumber = 10;
    modifyTheValue(myNumber, val-> val + incr);

    myNumber = 15;
    modifyTheValue(myNumber, val-> val * 10);
    modifyTheValue(myNumber, val-> val - 100);
    modifyTheValue(myNumber, val-> "somestring".length() + val - 100);
  }

  //API which accepts an implementation of 
  //Function interface
  static void modifyTheValue(int valueToBeOperated, 
          Function<Integer, Integer> function){
    int newValue = function.apply(valueToBeOperated);
    /*
     * Do some operations using the new value.
     */
    System.out.println(newValue);
  }

}

and the output is:

30
150
-85
-75

Note: The above code was compiled using the JDK downloaded from here and Netbeans 8 nightly builds.
 

Reference: Function interface – A functional interface in the java.util.function package in Java 8 from our JCG partner Mohamed Sanaulla at the Experiences Unlimited blog.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

4 Responses to "Function interface – A functional interface in the java.util.function package in Java 8"

  1. MrKlister says:

    Thanks for a great example! Seeing forward to this in java 8. I have an unrelated question though. You are using single quotes to define strings e.g. ‘somestring’ in your example. Is this also something new in java 8?

Leave a Reply


− 7 = two



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books