Sitemap
Shashank

Shashank

If it matters in gaming, I’m probably writing about it. News, guides, tips, tricks, updates, and more. 🎮🔥

Functional and Reactive Programming in Java

Functional and reactive programming are two distinct but complementary programming paradigms in Java, often used together for building modern, scalable, and responsive applications.

4 min readSep 11, 2025

--

Press enter or click to view image in full size
Functional Programming vs Reactive Programming in Java

Java has grown a lot from the days when it was just about object oriented programming. Starting with Java 8, it introduced functional programming features, and with tools like RxJava, Project Reactor, and Akka Streams, it also supports reactive programming.

In this article, we will explore what functional and reactive programming are, how Java makes them possible, where they come in handy, and we will also go through some easy coding examples.

Let’s go step by step….

1. Functional Programming in Java

What is Functional Programming?

Functional programming (FP) is a programming style where you think in terms of functions rather than objects. Instead of writing how something should be done step by step, you describe what should be done.

In FP, functions are treated as “first-class citizens,” which means you can pass them around as parameters, return them from methods, and store them in variables.

Key Features in Java:

Java was not originally built as a functional language, but from Java 8 onward, several features were introduced:

1. Lambda Expressions:

Small, anonymous functions that make code more concise.

Example:

List<String> names = Arrays.asList("John", "Alice", "Bob");
names.forEach(name -> System.out.println(name));

Instead of using loops, you directly express the action you want.

2. Functional Interfaces:

Interfaces with a single abstract method (e.g. Runnable, Comparator, Supplier, Consumer).

Example:

Predicate<Integer> isEven = n -> n % 2 == 0;
System.out.println(isEven.test(10)); // true

3. Streams API:

A functional way to process collections.

Example:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.forEach(System.out::println);

Here, we filter, transform, and print values in a declarative way instead of using multiple nested loops.

4. Optional Class:

Avoids null pointer exceptions and allows functional style chaining.

Optional<String> name = Optional.ofNullable("Alice");
name.ifPresent(System.out::println);

Advantages of FP in Java

  • Cleaner and more concise code.
  • Easier parallelization (streams can run in parallel).
  • Reduced side effects and improved readability.

2. Reactive Programming in Java

What is Reactive Programming?

Reactive programming is a way of handling asynchronous data streams. Instead of blocking threads and waiting for data, you react to changes as they happen.

Get Shashank’s stories in your inbox

Join Medium for free to get updates from this writer.

Think of it like Netflix: instead of downloading all movies before watching, you stream them as data comes in.

In Java, reactive programming is based on the Reactive Streams specification (introduced in Java 9 as Flow API), and frameworks like RxJava and Project Reactor implement it.

Core Principles of Reactive Programming:

  1. Asynchronous — Non-blocking code execution.
  2. Event-driven — Actions are triggered by events (e.g. data arrival, user input).
  3. Backpressure — Prevents systems from being overwhelmed by controlling the flow of data.
  4. Data Streams — Everything is considered as a stream of events.

Example of RxJava:

import io.reactivex.Observable;

public class ReactiveExample {
public static void main(String[] args) {
Observable.just("Apple", "Banana", "Orange")
.map(String::toUpperCase)
.subscribe(System.out::println);
}
}

Here:

  • Observable creates a data stream.
  • map transforms each item.
  • subscribe reacts to the stream and prints values.

Example with Project Reactor:

import reactor.core.publisher.Flux;

public class ReactorExample {
public static void main(String[] args) {
Flux.just(1, 2, 3, 4, 5)
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.subscribe(System.out::println);
}
}

This is very similar to RxJava but uses Flux and Mono (for multiple or single data items).

Java 9 Flow API:

Java 9 introduced the Flow API as part of java.util.concurrent. It defines four core interfaces:

  1. Publisher
  2. Subscriber
  3. Subscription
  4. Processor

This provides a standard base for reactive libraries to interoperate.

Functional vs Reactive Programming in Java

Press enter or click to view image in full size
Functional Programming vs Reactive Programming

Where to Use Them?

1. Functional Programming

  • When working with collections (filter, map, reduce).
  • Writing cleaner APIs with lambdas and streams.
  • Handling optional values instead of null checks.

2. Reactive Programming

  • Real time applications (chat apps, stock trading platforms).
  • Microservices communication.
  • Data pipelines where events keep flowing.

Functional and reactive programming are not replacements for object oriented programming in Java, they are powerful additions. Functional programming makes your code more declarative and concise, while reactive programming helps you handle asynchronous, event-driven data in a scalable way.

If you are building modern Java applications, combining these approaches can give you both cleaner code and better performance under load.

--

--

Shashank
Shashank

Written by Shashank

If it matters in gaming, I’m probably writing about it. News, guides, tips, tricks, updates, and more. 🎮🔥