A dedicated code repository and reference laboratory for mastering Java 8 Functional Programming.
Moving beyond traditional imperative Java, this repository covers the shift to declarative, concise, and clean code through hands-on implementations of Lambda Expressions, Single Abstract Method (SAM) contracts, and the complete java.util.function standard library.
src/
├── lambda/ # Lambda syntax, Runnable, Comparator, and lexical scope
├── functionalInterface/ # SAM contract, @FunctionalInterface, and interface inheritance
├── function/ # Built-in functional interfaces (Predicate, Function, Consumer, Supplier)
└── calculator/ # Lambda-driven arithmetic dispatch engine
Replacing verbose anonymous inner classes with concise lambda syntax and understanding runtime behavior:
- Syntax & Execution:
LambdaDemo.java— Core lambda syntax and parameter inference. - Multithreading:
LambdaRunnableDemo.java— Implementingjava.lang.Runnablewith zero boilerplate. - Sorting & Comparators:
LambdaComparatorDemo.java,ComparatorString.java— Custom collection sorting via inline lambda comparator definitions. - Scope &
thisContext:ThisDemo.java— Demonstrating lexical scoping differences: howthisrefers to the enclosing class in a lambda vs. the instance in an anonymous class.
Rules governing interfaces designed for lambda execution:
- SAM Principle:
MyInterface.java,FunctionalInterfaceDemo.java— Single Abstract Method constraints and@FunctionalInterfacevalidation. - Inheritance Semantics:
Parent.java,Child.java,FunctionalInterfaceInheritance.java— Rules for extending functional interfaces, overriding SAMs, and preserving functional contracts.
Comprehensive coverage of the java.util.function package:
| Interface | Method Signature | Purpose | Implemented In |
|---|---|---|---|
Predicate<T> |
boolean test(T t) |
Conditional evaluations & filtering logic | PredicateDemo.java |
Function<T, R> |
R apply(T t) |
Input-to-output data transformation | FunctionsDemo.java |
Consumer<T> |
void accept(T t) |
Side-effect execution without return | ConsumerDemo.java |
Supplier<T> |
T get() |
Lazy value generation / factory supplier | SupplierDemo.java |
BiPredicate<T, U> |
boolean test(T t, U u) |
Two-argument boolean condition | BiInterfacesDemo.java |
BiFunction<T, U, R> |
R apply(T t, U u) |
Two-argument data transformation | BiInterfacesDemo.java |
BiConsumer<T, U> |
void accept(T t, U u) |
Two-argument consumer operation | BiInterfacesDemo.java |
UnaryOperator<T> |
T apply(T t) |
Transformation where input & output types match | UnaryBinaryOperatorDemo.java |
BinaryOperator<T> |
T apply(T t1, T t2) |
Two arguments reduced to single result of same type | UnaryBinaryOperatorDemo.java |
- Demonstrates pipeline composition using
.andThen()(sequential execution) and.compose()(reversed precedence).
Calculator.java&Operations.java: Practical demonstration using functional interfaces as first-class citizens to perform dynamic arithmetic operations (+, -, *, /) without conditionalswitchstatements.
Any program can be compiled and executed directly from the terminal:
# Clone the repository
git clone https://github.com/alok0770/learning-java-8.git
cd learning-java-8
# Compile a specific file (e.g. FunctionChaining)
javac -d out src/function/FunctionChaining.java
# Run the compiled class
java -cp out function.FunctionChainingAlok Rana
- 🐙 GitHub: @alok0770
- 🌐 Portfolio: portfolio-alok-2b96.vercel.app
- 💼 LeetCode: @Alok_0770