Skip to content
IRC-Coding IRC-Coding
Functional Programming Lambda Expressions Functional Interfaces Streams Pure Functions Immutability map filter reduce Higher Order Functions

Functional Programming with Lambda Explained

Learn functional programming with lambdas, functional interfaces, and streams. Pure functions, immutability, higher-order functions, map, filter, reduce.

S

schutzgeist

2 min read
Functional Programming with Lambda Explained

Functional Programming Lambda – Functional Interfaces, Streams, map, filter, reduce

This post is a term explanation of functional programming – including exam questions and tags.

In a Nutshell

Functional programming focuses on computation through functions rather than state changes; lambda expressions are inline function literals that in Java are bound to the runtime through Functional Interfaces, which have exactly one abstract method.

Compact Technical Description

A lambda expression describes anonymous behavior with parameters, body, and optional return type; the type is inferred from the target context, target type, such as a Functional Interface like Predicate, Function, Consumer, Supplier. Functional Interfaces enable Higher Order Functions, where functions are passed as values, returned, stored. Core principles are Pure Functions, where immutable input leads to deterministic output without side effects; referential transparency and immutability reduce coupling and facilitate testing and parallelization.

Exam-Relevant Key Points

  • Know lambda syntax, parameter list, arrow, body, target type through Functional Interface
  • Standard interfaces, Function T,R, Predicate T, Consumer T, Supplier T, UnaryOperator T, BinaryOperator T
  • Streams, map, filter, flatMap, reduce, collect, briefly explain and justify typical complexity
  • IHK relevant, differences between imperative vs. functional, avoid side effects, advantages for testability and parallelization
  • Practice, method references, ClassName::staticMethod, instance::method, Constructor::new
  • Security aspect, no hidden side effects in lambdas, thread safety through immutability
  • Cost-effectiveness, less boilerplate, clearer data flow, better maintainability, potentially fewer bugs
  • Documentation obligation, function contract, input domain, side effects, concurrency, complexity

Core Components

  1. Lambda expression syntax, parameters, body, return
  2. Functional Interface with exactly one abstract method, SAM
  3. Method references as compact notation
  4. Higher Order Functions, functions as parameters or return values
  5. Pure Function and side effects, testability, determinism
  6. Immutability and thread safety
  7. Stream Pipeline, Lazy Evaluation, short-circuit operators
  8. Closure and effectively final in Java
  9. Optional and error patterns, Optional, Try, Either concepts
  10. Parallel Streams, data independence, Boxed vs. Primitive Streams

Practical Example

// Java, filter and transform data with lambdas and streams
List<String> names = List.of("Mila", "Tom", "Amir", "Mara")
List<Integer> lengths = names.stream()
.filter(n -> n.startsWith("M"))
.map(String::length)
.sorted()
.toList()

// Custom Functional Interface and lambda
@FunctionalInterface
interface IntOp { int apply(int a, int b) }
IntOp add = (a, b) -> a + b
IntOp max = Math::max
int r1 = add.apply(3, 4) // 7
int r2 = max.apply(5, 9) // 9

Explanation: The stream pipeline demonstrates map, filter, sorted, toList; the lambdas are pure and side-effect-free.

Advantages and Disadvantages

Advantages

  • Less boilerplate, clearer declarative style
  • Better testability through Pure Functions
  • Easy parallelization thanks to immutability
  • High reusability through combinators

Disadvantages

  • Learning curve with abstractions like Higher Order Functions
  • Debugging in pipelines more difficult
  • Careless captures can bind memory
  • In Java limitation through effectively final and Erasure

Typical Exam Questions (with Short Answer)

  1. Lambda expression in Java and type determination? Anonymous function literal; the type is derived from the target context, a Functional Interface with one abstract method.

  2. Four central Functional Interfaces from java.util.function? Function T,R, Predicate T, Consumer T, Supplier T; additionally operators for T to T.

  3. Method as lambda vs. method reference? Method references point directly to existing methods; they are equivalent to a lambda, but shorter and more readable.

  4. Effectively final with closures? Local variables used in lambdas must not be modified after assignment; the JVM can safely capture them.

  5. Referential transparency and why important? An expression can be replaced by its value without changing program behavior; promotes testability and reasoning.

  6. How does reduce work in a stream pipeline? Combines elements iteratively with an associative accumulator and optionally an identity, e.g., reduce(0, Integer::sum).

  7. Risks with Parallel Streams? Non-associative accumulators lead to incorrect results; side-effect-laden lambdas are unsafe.

  8. Model errors functionally? Through values like Optional for absence, Try or Either for success or failure, instead of throwing global Exceptions.

  9. Imperative loop vs. functional pipeline? Imperative: explicit mutation and control; functional: declarative data flow, immutability, easier to test.

  10. Functional Interfaces vs. true function types? Java uses SAM types as a substitute, while languages like Kotlin, Scala, Haskell have native function types with type arity.

Most Important Sources

  1. https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
  2. https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
  3. https://en.wikipedia.org/wiki/Functional_programming
Back to Blog
Share:

Related Posts