Java 8 Examples

Java 8 Function Interface Example

Posted On
Posted By admin

In this blog post, I will be explaining how the Java 8 functional interface Function works. To know more about functional interfaces, you can refer this blog post.

The Function interface provides a method called apply. It accepts a single parameter of any data type and returns a result of any data type. So it basically applies the logic in the apply method to the input parameter and returns the result.

Function Interface Example with String Input and Integer return type

Consider the following code snippet:


public class FunctionDemo {
public static void main(String[] args) {
Function<String,Integer> lengthChecker = (str) -> {return str.length();};
String input = "Hello World";
System.out.println("Length of "+input+" is "+lengthChecker.apply(input) );
input = "Test";
System.out.println("Length of "+input+" is "+lengthChecker.apply(input) );

}

}

Here, we have written a Function implementation that accepts a String value and returns its length. The Function.apply method is implemented using a lambda expression. This expression accepts a String value and returns its length. So when you execute this code, it will print the following output:

Length of Hello World is 11
Length of Test is 4

Function Interface Example with Object input and String output

Suppose there is a Book class as follows:


public class Book {

private String bookName;
private String bookDesc;

public Book(String bookName,String bookDesc){
this.bookName = bookName;
this.bookDesc = bookDesc;
}

//Getter and setter methods

}

And consider the following code snippet:


public class FunctionDemoWithBook {

public static void main(String[] args) {
List<Book> books = new ArrayList<Book>();
books.add(new Book("Book1","Book Desc1"));
books.add(new Book("Book2","Book Desc2"));
books.add(new Book("Book3","Book Desc3"));

Function<Book,String> bookNameRetriever = (book) -> {return book.getBookName();};

List<String> bookNames = new ArrayList<String>();
for(Book book:books){
bookNames.add(bookNameRetriever.apply(book));
}
System.out.println("Book names are:"+bookNames);
}

}

Here, we are creating a List of Book objects and adding them to the books list.  We are then defining a bookNameRetriever function. This accepts a Book object and returns its name.

We are then using the bookNameRetriever to generate a List of Book names. So we are iterating through the Books list and invoking the apply method on each book to retrieve the name of the Book. When you run this code, it will print the following output:

Book names are:[Book1, Book2, Book3]

You can get the source code for this example along with the code for other Java 8 examples at the Github repository here.


 

If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my new course Learn Java 8 New Features

If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment