Skip to content

Latest commit

 

History

History
105 lines (86 loc) · 3.9 KB

File metadata and controls

105 lines (86 loc) · 3.9 KB

🚀 Java Functional Programming with Lambda & Streams

This repository demonstrates Java Functional Programming concepts using Lambda Expressions, Streams API, and Method References with concise, real-world examples.


📌 What is Functional Programming?

Functional Programming focuses on writing declarative, immutable, and side-effect-free code using functions as first-class citizens.


🔹 Lambda Expressions ✨

  • Lambda Expression – A concise way to implement functional interfaces using anonymous functions.
  • Syntax(parameters) -> expression or (parameters) -> { statements }.
  • Functional Interface – An interface with exactly one abstract method.

🔹 Built-in Functional Interfaces 🧩

  • 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 API 🌊

Streams enable functional-style processing of collections using a pipeline of operations.

⚙️ Intermediate 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.

🏁 Terminal Operations

  • 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.

🔹 Method References 🔗

  • 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 Class 🛡️

  • 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.

🎯 Key Benefits 🚀

  • Cleaner and more readable code
  • Less boilerplate and fewer bugs
  • Easy parallel processing with streams
  • Improved maintainability and scalability

👨‍💻 Author

Suvam Debnath