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