0% found this document useful (0 votes)
0 views2 pages

Java Functional Programming Quick Reference

The document provides a quick reference to Java functional programming concepts, including the use of Optional to avoid NullPointerExceptions, method references as shorthand for lambdas, and the definition of functional interfaces. It also covers lambda expressions, stream operations, custom functional interfaces, function chaining, and predicate logic composition. Finally, it offers guidance on when to use each concept effectively.

Uploaded by

Aman Pal Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

Java Functional Programming Quick Reference

The document provides a quick reference to Java functional programming concepts, including the use of Optional to avoid NullPointerExceptions, method references as shorthand for lambdas, and the definition of functional interfaces. It also covers lambda expressions, stream operations, custom functional interfaces, function chaining, and predicate logic composition. Finally, it offers guidance on when to use each concept effectively.

Uploaded by

Aman Pal Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Functional Programming Concepts

– Quick Reference
1. Optional
Purpose: Avoid NullPointerException and express absence of value clearly.

 Use Optional<T> for return types, not fields or parameters.


 Common methods: of(), empty(), get(), isPresent(), orElse(), orElseGet(), map(), flatMap(),
ifPresent().
 Avoid get() without isPresent().

2. Method References
Purpose: Shorthand for lambdas when method already exists.

Types:

 Static: ClassName::staticMethod (e.g., Integer::parseInt)


 Instance on object: object::instanceMethod (e.g., System.out::println)
 Instance on type: ClassName::instanceMethod (e.g., String::toLowerCase)

Why ClassName::instanceMethod works:

 String::toLowerCase is shorthand for str -> str.toLowerCase()


 It calls the method on each object of the stream.

3. Lambda Expressions
Purpose: Inline implementation of functional interfaces.

 Syntax: (param1, param2) -> expression


 Example: x -> x * 2; () -> System.out.println("Hello")

4. Functional Interfaces
An interface with exactly one abstract method.

Common built-in types:

 Function<T, R>: R apply(T)


 Predicate<T>: boolean test(T)
 Consumer<T>: void accept(T)
 Supplier<T>: T get()
 UnaryOperator<T>: T apply(T)
 BinaryOperator<T>: T apply(T, T)

Use negate(), and(), or() on Predicates for logic inversion or composition.

5. Stream + Lambda + Method Reference


 map(), filter(), collect() used with lambda or method reference.
 Example: stream.map(String::toUpperCase).forEach(System.out::println);

6. Custom Functional Interfaces


 Define with @FunctionalInterface annotation.
 Can have parameters and return types.
 Example: interface Printer { void print(String s); }

7. Function Chaining
 Function chaining with andThen(), compose().
 Example: trim.andThen(toUpper).apply(" abc ") -> "ABC"

8. Predicate Logic Composition


 Negate: p.negate()
 Combine: p1.and(p2), p1.or(p2)
 Example: isShort.and(startsWithA).test("Ami")

9. When to Use What


 Use Optional<T> for nullable returns.
 Use lambda for inline functional behavior.
 Use method reference if method already exists.
 Use Stream for processing collections in a pipeline.

You might also like