Skip to content

This repository covers Functional Programming concepts in Java, focusing on Lambda Expressions, Streams API, and method references.

Notifications You must be signed in to change notification settings

Suvam-Debnath/FunctionalProgramming

Repository files navigation

πŸš€ 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

About

This repository covers Functional Programming concepts in Java, focusing on Lambda Expressions, Streams API, and method references.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages