Introdution to Functional Programming and Lambda Expressions

In functional programming, we think in terms of functions. While Object Oriented programming abstract over data, functional programming abstract over behavior. Instead of data, stateless functions are be passed across the code. 

Lambda expressions are Java's way of doing functional programming. Lambda expressions in computer science are anonymous functions: a function (or a subroutine) defined, and possibly called, without being bound to an identifier.

Lambda Expressions

Lambda expressions in Java provide an easy and precise way to implement an interface with only one single abstract method without referring to the method name at all.  Since the interface has only one single method (function), passing across that interface implementation gives the impression of passing across that function.

 

Quick Example:

Runnable implementation without lambda expression:

Runnable r = new Runnable() {

  @Override

  public void run()

  {

    System.out.println("run");

  }

}

Runnable implementation with lambda expression:

Runnable r = () -> System.out.println("run");

Most of the things are inferred by Java here.

 

Important points about lambda expressions can be summarized as:

  1. The target type of a lambda expression is the type of the context in which lambda expression is defined. E.g. In 'Runnable r = () -> System.out.println("run");' the right hand side of the equals operator represents a lambda expression and its type is Runnable.

  2. If a lambda expression is passed as a method parameter, the type will be of that method parameter.

  3. Lambda expressions are Java's way of doing functional programming. 

  4. Lambda expressions can only appear in places where they will be assigned to a variable whose type is a functional interface: E.g. In 'Runnable r = () -> System.out.println("run");' Runnable is a functional interface.​

 

Functional Interfaces

Lambda expressions in Java are actually implementations of a functional interface. 

Functional interfaces are interfaces with only a single abstract method, that is used as the type of a lambda expression.

Important points about functional interfaces can be summarized as follows:

  1. Optionally, you can use the informative annotation type @FunctionalInterface annotation over an interface to indicate that it is a functional interface.

  2. Instances of functional interfaces can be created with lambda expressions, method references, or constructor references. 

  3. Default methods or any abstract method overriding one of the public methods of java.lang.Object, are not counted towards the single abstract method requirement for becoming a functional interface.

  4. Already existing interfaces Runnable, Callable,comparator,TimeTask etc. are functional interfaces.

  5. Java 8 introduces few general purpose functional interfaces based on the input and output to the single abstract method. Most common ones are Predicate<T>, Consumer<T>, Function<T,R>, Supplier<T>, UnaryOperator<T> and BinaryOperator<T,T>. We will see some examples for these soon.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)