Techdozo
  • Microservices
    • All
    • GraphQL
    • gRPC
    • Spring Boot
    GraphQL Error Handling

    GraphQL Error Handling

    Pages

    Spring for GraphQL: Pagination with Code Example

    Spring for GraphQL: Interfaces and Unions

    gRPC Bidirectional Streaming with Code Example

    gRPC Bidirectional Streaming with Code Example

    gRPC Client Streaming

    gRPC Client Streaming

    Distributed transaction in microservices using Saga

    Distributed Transactions in Microservices: implementing Saga with Temporal

  • Spring Boot
    Pages

    Spring for GraphQL: Pagination with Code Example

    Spring Boot GraphQL service

    Getting started with Spring Boot GraphQL service

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    RESTful Microservices with Spring Boot and Kubernetes

    RESTful Microservices with Spring Boot and Kubernetes

    RESTful API Gateway with gRPC

    RESTful API Gateway with gRPC

  • gRPC
    gRPC Bidirectional Streaming with Code Example

    gRPC Bidirectional Streaming with Code Example

    gRPC Client Streaming

    gRPC Client Streaming

    gRPC Interceptor: unary interceptor with code example

    gRPC Interceptor: unary interceptor with code example

    gRPC: synchronous and asynchronous Server streaming RPC

    gRPC: synchronous and asynchronous Server streaming RPC

    Photo by Ramón Salinero on Unsplash

    gRPC: synchronous and asynchronous unary RPC in Java

    Microservices inter-process communication using gRPC

    gRPC for microservices communication

  • GraphQL
    GraphQL Error Handling

    GraphQL Error Handling

    Pages

    Spring for GraphQL: Pagination with Code Example

    Spring for GraphQL: Interfaces and Unions

    GraphQL Directive

    GraphQL Directive

    Spring for GraphQL mutation

    Spring for GraphQL: Mutation

    Spring for GraphQL: How to solve the N+1 Problem?

    Spring for GraphQL: How to solve the N+1 Problem?

    Spring GraphQL with @Controller, @SchemaMapping and @QueryMapping

    Spring for GraphQL : @SchemaMapping and @QueryMapping

    Spring Boot GraphQL service

    Getting started with Spring Boot GraphQL service

  • Kubernetes
    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Components of Kubernetes Architecture

    Components of Kubernetes Architecture

    Helm Chart: quick start your app deployment on Kubernetes

    Helm Chart: quick start your app deployment on Kubernetes

    gRPC load balancing on Kubernetes (using Headless Service)

    gRPC load balancing on Kubernetes (using Headless Service)

    Getting started with Kind: quick start a multi-node local Kubernetes cluster

    Getting started with Kind: quick start a multi-node local Kubernetes cluster

    Getting started with Minikube: deploying application on local Kubernetes cluster

    Getting started with Minikube: deploying application on local Kubernetes cluster

  • Java
    Java Streams: Stream Operation with Examples

    Java Streams: Stream Operation with Examples

    Java Streams: stream creation with examples

    Java Streams: stream creation with examples

    Garbage Collection

    Super Fast Garbage Collectors in Java

    Calculus

    Functional Programming in Java

No Result
View All Result
  • Login
  • Microservices
    • All
    • GraphQL
    • gRPC
    • Spring Boot
    GraphQL Error Handling

    GraphQL Error Handling

    Pages

    Spring for GraphQL: Pagination with Code Example

    Spring for GraphQL: Interfaces and Unions

    gRPC Bidirectional Streaming with Code Example

    gRPC Bidirectional Streaming with Code Example

    gRPC Client Streaming

    gRPC Client Streaming

    Distributed transaction in microservices using Saga

    Distributed Transactions in Microservices: implementing Saga with Temporal

  • Spring Boot
    Pages

    Spring for GraphQL: Pagination with Code Example

    Spring Boot GraphQL service

    Getting started with Spring Boot GraphQL service

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    RESTful Microservices with Spring Boot and Kubernetes

    RESTful Microservices with Spring Boot and Kubernetes

    RESTful API Gateway with gRPC

    RESTful API Gateway with gRPC

  • gRPC
    gRPC Bidirectional Streaming with Code Example

    gRPC Bidirectional Streaming with Code Example

    gRPC Client Streaming

    gRPC Client Streaming

    gRPC Interceptor: unary interceptor with code example

    gRPC Interceptor: unary interceptor with code example

    gRPC: synchronous and asynchronous Server streaming RPC

    gRPC: synchronous and asynchronous Server streaming RPC

    Photo by Ramón Salinero on Unsplash

    gRPC: synchronous and asynchronous unary RPC in Java

    Microservices inter-process communication using gRPC

    gRPC for microservices communication

  • GraphQL
    GraphQL Error Handling

    GraphQL Error Handling

    Pages

    Spring for GraphQL: Pagination with Code Example

    Spring for GraphQL: Interfaces and Unions

    GraphQL Directive

    GraphQL Directive

    Spring for GraphQL mutation

    Spring for GraphQL: Mutation

    Spring for GraphQL: How to solve the N+1 Problem?

    Spring for GraphQL: How to solve the N+1 Problem?

    Spring GraphQL with @Controller, @SchemaMapping and @QueryMapping

    Spring for GraphQL : @SchemaMapping and @QueryMapping

    Spring Boot GraphQL service

    Getting started with Spring Boot GraphQL service

  • Kubernetes
    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Components of Kubernetes Architecture

    Components of Kubernetes Architecture

    Helm Chart: quick start your app deployment on Kubernetes

    Helm Chart: quick start your app deployment on Kubernetes

    gRPC load balancing on Kubernetes (using Headless Service)

    gRPC load balancing on Kubernetes (using Headless Service)

    Getting started with Kind: quick start a multi-node local Kubernetes cluster

    Getting started with Kind: quick start a multi-node local Kubernetes cluster

    Getting started with Minikube: deploying application on local Kubernetes cluster

    Getting started with Minikube: deploying application on local Kubernetes cluster

  • Java
    Java Streams: Stream Operation with Examples

    Java Streams: Stream Operation with Examples

    Java Streams: stream creation with examples

    Java Streams: stream creation with examples

    Garbage Collection

    Super Fast Garbage Collectors in Java

    Calculus

    Functional Programming in Java

No Result
View All Result
Techdozo
No Result
View All Result
Home Java

Functional Programming in Java

Pankaj by Pankaj
September 7, 2020
in Java
Reading Time: 8 mins read
0
A A
0
Calculus
ADVERTISEMENT

Functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that each return a value, rather than a sequence of imperative statements which change the state of the program

wikipedia

Java 8 introduced functional programming in the form of Lambda. The term Lambda comes from lambda calculus, which is used to describe computations.

Lambda

We can think of a lambda expression as an anonymous function that can be assigned to a variable and passed to a method, which accepts a functional interface as an argument. Lambda expression doesn’t have a name, but it has a list of parameters, a body, and a return type.

ADVERTISEMENT

(parameters) -> expression

A lambda expression can be used in the context of a functional interface.

Functional Interface

A functional interface is an interface that specifies exactly one abstract method.

public interface Comparator<T> {                           
    int compare(T o1, T o2);
}
public interface Runnable {                                
    void run();
}

Lambda expressions let us implement the abstract method of a functional interface directly inline and treat the whole expression as an instance of a functional interface.

Function Descriptor

We call the signature of the abstract method of the functional interface a function descriptor. The function descriptor describes the signature of the lambda expression. For example, we can think of the function descriptor of Runnable as () -> void as it has one abstract method which accepts nothing and returns nothing (void).

Java Functional Interface

Predicate

The java.util.function.Predicate<T> interface defines an abstract method named test that accepts an object of generic type T and returns a boolean. This interface can be used to represent a boolean expression that uses an object of type T.

Function Descriptor : T -> boolean

ADVERTISEMENT
@FunctionalInterface
public interface Predicate<T> {
    boolean test(T t);
}

Consumer

The java.util.function.Consumer<T> interface defines an abstract method named accept that takes an object of generic type T and returns no result (void). We can use this interface when we need to access an object of type T and perform some operations on it.

Function Descriptor : T -> void

Function

The java.util.function.Function<T, R> interface defines an abstract method named apply that takes an object of generic type T as input and returns an object of generic type R. We can use this interface when we need to define a lambda that maps information from an input object to output.

Function Descriptor : T -> R

Supplier

The java.util.function.Supplier<T> interface defines an abstract method named get that takes nothing and returns an object of type T.

Function Descriptor : () -> R

Primitive Specializations

Primitive interfaces are specialized interfaces to avoid autoboxing operations when the inputs or outputs are primitives.

public interface IntPredicate {
    boolean test(int t);
}

Type Checking

The type of lambda is deduced from the context in which the lambda is used. The type expected for the lambda expression inside the context (for example, a method parameter that it’s passed to or a local variable that it’s assigned to) is called the target- type. Lambda expression can get their target type from an assignment context, method-invocation context (parameters and return), and a cast context.

Object o = (Runnable) () -> System.out.println("Hello");

Capturing lambdas

Lambdas can capture (to reference in their bodies) instance and static variables without restrictions. But when local variables are captured, they have to be explicitly declared final or be effectively final.

Why do we have this restriction?

Instance variables are stored on the heap, whereas local variables live on the stack. Suppose a lambda could access the local variable directly, and the lambda was used in a thread. In that case, the thread using lambda could try to access the variable after the thread that allocated the variable had deallocated it. Hence, Java implements access to a free local variable as access to a copy rather than access to the original variable. This makes no difference if the local variable is assigned to only once—hence the restriction.

Method References

There are three main kinds of method references:

  • A method reference to a static method. Example, – Integer::parseInt
  • A method reference to an instance method of an arbitrary type. Example – String::length
  • A method reference to an instance method of an existing object or expression. Example – student::getRank where the student is the local variable of Type Student which has method getRank()
List<String> list = Arrays.asList("a","b","A","B");
list.sort((s1, s2) -> s1.compareToIgnoreCase(s2));

can be written as

List<String> list = Arrays.asList("a","b","A","B");
list.sort(String::compareToIgnoreCase);

Constructor References

Reference to the existing constructor can be done using ClassName::new

Supplier<List<String>> supplier = ArrayList::new; is same as Supplier<List<String>> supplier = () -> new ArrayList<>();

Composing Lambda

Many functional interfaces contain default methods that can be used to compose lambda expressions. Example of composition –

  • Combine two predicates into a larger predicate that performs an or operation between the two predicates
  • Reverse or chain comparator

Comparators

Sorting students based on Rank in reverse order

Comparator<Student> c = Comparator.comparing(Student::getRank);
students.sort(comparing(Student::getRank).reversed()); 

Sorting students based on name (reverse) and then Rank in reverse order

students.sort(comparing(Student::getName).reversed()
        .thenComparing(Student::getRank)); 

Predicates

The Predicate interface includes three methods negate, and, and or , which can be use to create more complicated predicated.

Predicate<Integer> naturalNumber = i -> i > 0;                                     
Predicate<Integer> naturalNumberLessThanHundred = naturalNumber.and( i -> i < 100);

Functions

The Function interface comes with two default methods andThen and compose.

Consider f(x) = x2 and g(x) = x3 + 1 then

g(f(x)) ->

Function<Integer,Integer> square = n -> n*n;                         
Function<Integer,Integer> squareAndCube = square.andThen(n -> n*n*n+1);
System.out.println(squareAndCube.apply(2));  
65                        

f(g(x)) ->

Function<Integer,Integer> square = n -> n*n;                              
Function<Integer,Integer> squareAndCube = square.compose(n -> n*n*n + 1); 
System.out.println(squareAndCube.apply(2));                               

Applying Lambda

Let’s see how we can write a generic method to filter a collection of books based on certain attributes (think of this as a where clause of SQL).

public static List<Book> filter(Predicate<Book> where) {                
  List<Book> books = Catalogue.books();                                 
  return books.stream().filter(where).collect(Collectors.toList());     
}                                                                       

Lambda expression to filter different books by different filter

List<Book> javaBook = filter(book -> book.getCategory().equals(JAVA));               
List<Book> joshuaBlochBook = filter(book -> book.getAuthor().equals("Joshua Bloch"));

Summary

A lambda expression can be considered an anonymous function that can be passed around which can be used in the context of a functional interface. A functional interface is an interface that specifies exactly one abstract method.

Tags: functional programmingjavalambda
Next Post

Super Fast Garbage Collectors in Java

Pankaj

Pankaj

Software Architect @ Schlumberger ``` Cloud | Microservices | Programming | Kubernetes | Architecture | Machine Learning | Java | Python ```

Related Posts

Java Streams: Stream Operation with Examples
Java

Java Streams: Stream Operation with Examples

May 8, 2021
Java Streams: stream creation with examples
Java

Java Streams: stream creation with examples

May 1, 2021
Garbage Collection
Java

Super Fast Garbage Collectors in Java

September 14, 2020

Discussion about this post

Recent Articles

GraphQL Error Handling

GraphQL Error Handling

August 9, 2023
Pages

Spring for GraphQL: Pagination with Code Example

July 26, 2023

Spring for GraphQL: Interfaces and Unions

May 17, 2023
gRPC Bidirectional Streaming with Code Example

gRPC Bidirectional Streaming with Code Example

February 17, 2023
  • Trending
  • Comments
  • Latest
gRPC Bidirectional Streaming with Code Example

gRPC Bidirectional Streaming with Code Example

February 17, 2023
Deploying a RESTful Spring Boot Microservice on Kubernetes

Deploying a RESTful Spring Boot Microservice on Kubernetes

August 18, 2021
gRPC Interceptor: unary interceptor with code example

gRPC Interceptor: unary interceptor with code example

April 30, 2022
Temporal Workflow Orchestration

Workflow Orchestration with Temporal and Spring Boot

October 29, 2022
Calculus

Functional Programming in Java

0
Java Streams: stream creation with examples

Java Streams: stream creation with examples

0
Garbage Collection

Super Fast Garbage Collectors in Java

0
Java Streams: Stream Operation with Examples

Java Streams: Stream Operation with Examples

0
GraphQL Error Handling

GraphQL Error Handling

August 9, 2023
Pages

Spring for GraphQL: Pagination with Code Example

July 26, 2023

Spring for GraphQL: Interfaces and Unions

May 17, 2023
gRPC Bidirectional Streaming with Code Example

gRPC Bidirectional Streaming with Code Example

February 17, 2023
Facebook Twitter Pinterest

TECHDOZO

Simplifying modern tech stack!

Browse by Category

  • Bitesize
  • GraphQL
  • gRPC
  • Java
  • Kubernetes
  • Microservices
  • Spring Boot

Recent Articles

GraphQL Error Handling

GraphQL Error Handling

August 9, 2023
Pages

Spring for GraphQL: Pagination with Code Example

July 26, 2023

© 2023 Techdozo.

Welcome Back!

Sign In with Google
OR

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result
  • Home
  • gRPC
  • Kubernetes
  • Microservices
  • GraphQL

© 2023 Techdozo.