Skip to main content

Functional programming in Java

Ayesha
EditReport

Functional Programming in Java

Functional Programming in Java is a style of writing code where you focus on functions and immutability (not changing things). It is a way of programming that treats computation like a mathematical function and avoids changing data or using variables that can change over time.. Java introduced functional programming features in Java 8, including lambdas, streams, and functional interfaces.

Video Explanationโ€‹

Table of Contentsโ€‹


Introductionโ€‹

Functional programming focuses on using functions to process data, avoid side effects, and work with immutable data. Java 8 introduced lambda expressions, streams for a more functional approach to programming.


Key Conceptsโ€‹

Immutabilityโ€‹

Immutability means that once an object is created, it cannot be modified. Functional programming encourages immutable data to avoid side effects and make code more predictable.

public final class Person {
private final String name;
private final int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

// No setters, only getters
}

Higher Order Functionsโ€‹

Functions that take other functions as arguments or return functions are called higher-order functions.

Function<Integer, Integer> add = x -> x + 1;
Function<Integer, Integer> multiply = x -> x * 2;

Pure Functionsโ€‹

A function is pure if it always produces the same output for the same input and has no side effects.

public int add(int a, int b) {
return a + b; // Always returns the same result
}

Java Functional Programming Featuresโ€‹

Lambda Expressionsโ€‹

Lambda expressions enable you to pass behavior as arguments to methods, making your code more readable and flexible, especially in contexts like streams and event handling.

Syntax of a Lambda Expression in Java:โ€‹

(parameters) -> expression

Exampleโ€‹

(a, b) -> a + b  // A lambda expression that adds two numbers

Streams APIโ€‹

The Streams API in Java, introduced in Java 8, is a powerful and flexible feature that enables functional-style operations on sequences of elements, such as collections (e.g., List, Set, Map). It allows you to process collections of data in a declarative way, making the code more readable, concise, and expressive.

Syntaxโ€‹

// From a collection
Stream<T> stream = collection.stream();

Exampleโ€‹

List<Integer> numbers = List.of(1, 2, 3, 4, 5);
int sum = numbers.stream()
.filter(n -> n % 2 == 0) // Filter even numbers
.map(n -> n * 2) // Double each number
.reduce(0, (a, b) -> a + b); // Sum them up

Next Stepsโ€‹

For a complete guide with detailed examples on lambda syntax, method references, filtering, mapping, and DSA-style stream patterns, see Java Streams API and Lambda Expressions.

Conclusionโ€‹

Java's functional programming features such as lambda expressions and the Streams API allow for more concise and readable code.

Telemetry Integration

Completed working through this block? Sync progress to workspace.