Functional interface in java8.

            Interface is the one which has only abstract methods. It has only declaration. It implements the concept of abstraction. Functional interfaces are important concept in java 8.

What is functional interface??

              Functional interface has only one abstract method. This uses lambda expressions to implement the method.

Built in package: java.util.function

Common functional interfaces:

The below functional interfaces are commonly used from java SE1.8 onwards.

  • ActionListener, Runnable, Callable, Comparable.

Simple program to implement functional interface using Runnable:

class Test {

    public static void main(String args[])

    {

      Runnable runnable = () -> System.out.println("A threat is running separately");

      new Thread(runnable).start();

    }

}

Java 8 Functional interfaces:

              These are the functional interfaces are included in java8 for performing in multiple situations.

  1. Consumer
  2. Predicate
  3. Function 
  4. Supplier
  5. Consumer -> Bi-Consumer
  6. Predicate -> Bi-Predicate
  7. Function -> Bi-Function, Unary Operator, Binary Operator 

Java Program for implementing functional interface for Book information System:

·       Create a book class with four elements. Id, Name, price and Description.

·       Using constructors, initialise the values.

·       Create the object for Book class and add the value.

class Book

{

    int id;

    String name;

    double price;

     String specialization;

     public Book(int id, String name, double percentage, String specialization)

    {

        this.id = id;

        this.name = name;

         this.price = price;

        this.specialization = specialization;

    }

     public int getId() {

        return id;

    }

     public String getName() {

        return name;

    }

     public double getPrice() {

        return price;

    }

     public String getSpecialization() {

        return specialization;

    }

     @Override

    public String toString()

    {

        return id+"-"+name+"-"+price+"-"+specialization;

    }

}

 

List<Book> listOfBooks = new ArrayList<Book>();

listOfBooks.add(new Book(11, "Let us C",250.15 , "computer Science"));

listOfBooks.add(new Book(11, "Let us C++",315 , "computer Science"));

listOfBooks.add(new Book(11, "Learn Java Programming ",475 , "computer Science"));

listOfBooks.add(new Book(11, "Python programming",370 , "computer Science"));

listOfBooks.add(new Book(11, "SQL Programmimg",276 , "computer Science"));

 

How to use functional interfaces for this class “Book”:

1.Predicate:

This functional interface  used to Check a specific book.

For example,

Predicate <Book> Programming = (Book book)-> book.getSpecialization.equals(“Java Programming”);

List<Book> Programming = new ArrayList<Book>();

for (Book book : listOfBooks)

{

    if (Programming.test(book))

    {

        Programming.test(book);

    }

}

 

2. Consumer :

              Using this functional interface,we display the prices of all books.

Consumer<Book> PriceList = (Book book) -> {

        System.out.println(book.getName()+" : "+book.getPrice());

    };

         

for (Book book : listOfBook)

{

    price.accept(book);

}

3. Function:

              This functional interface extracts the book name alone from the system.

Function<Book, String> bookNameFunction = (Book book) -> Book.getName();

List<String> BookNames = new ArrayList<String>();         

for (Book book : listOfBooks)

{

    bookNames.add(bookNameFunction.apply(book));

}

4. Supplier :

 This is used to  Create a new book information.

Supplier<Book>  bookSupplier = () -> new Book(666, "Perl Programming", 921.45, "Computer Programming");

listOfBooks.add(bookSupplier.get());

These are the ways to use functional interfaces in java 8.

No comments:

Post a Comment