This repository demonstrates Java Functional Programming concepts using Lambda Expressions, Streams API, and Method References with concise, real-world examples.
Functional Programming focuses on writing declarative, immutable, and side-effect-free code using functions as first-class citizens.
- Lambda Expression – A concise way to implement functional interfaces using anonymous functions.
- Syntax –
(parameters) -> expressionor(parameters) -> { statements }. - Functional Interface – An interface with exactly one abstract method.
- Predicate – Takes one input and returns a boolean.
- Function – Takes one input and returns a result.
- Consumer – Takes one input and performs an action without returning a result.
- Supplier – Takes no input and returns a value.
- BiPredicate – Takes two inputs and returns a boolean.
- BiFunction – Takes two inputs and returns a result.
- BiConsumer – Takes two inputs and performs an action without returning anything.
Streams enable functional-style processing of collections using a pipeline of operations.
- filter() – Selects elements that match a condition.
- map() – Transforms each element into another form.
- flatMap() – Flattens nested structures into a single stream.
- distinct() – Removes duplicate elements.
- sorted() – Sorts elements in natural or custom order.
- limit() – Restricts the number of elements in a stream.
- skip() – Skips the specified number of elements.
- forEach() – Performs an action for each element.
- collect() – Converts the stream into a collection or final result.
- reduce() – Combines elements into a single value.
- count() – Returns the number of elements.
- anyMatch() – Checks if any element matches a condition.
- allMatch() – Checks if all elements match a condition.
- noneMatch() – Checks if no elements match a condition.
- findFirst() – Returns the first element of the stream.
- findAny() – Returns any element from the stream.
- Static Method Reference – Refers to a static method using
ClassName::methodName. - Instance Method Reference – Refers to an instance method using
object::methodName. - Constructor Reference – Refers to a constructor using
ClassName::new.
- Optional – A container object used to avoid NullPointerException.
- isPresent() – Checks if a value exists.
- orElse() – Returns a default value if empty.
- orElseGet() – Returns a value from a Supplier if empty.
- orElseThrow() – Throws an exception if the value is absent.
- Cleaner and more readable code
- Less boilerplate and fewer bugs
- Easy parallel processing with streams
- Improved maintainability and scalability
Suvam Debnath