Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Lambda

Functional Programming Concepts

Pure Functions:

Pure functions are functions that always produce the same output for the same input and have no side effects. They do not modify any external state or variables and have no dependencies on the state of the system. Pure functions are deterministic and predictable.

Example of a Pure Function:

import java.util.function.Function;

public class PureFunctionExample {
    public static void main(String[] args) {
        Function<Integer, Integer> addTwo = number -> number + 2;
        int result = addTwo.apply(3); // Output: 5
        System.out.println(result);
    }
}

In the above example, the addTwo function is a pure function because it always produces the same output (input + 2) for the same input.

Immutability:

Immutability means that once an object is created, its state cannot be changed. In functional programming, immutable objects are preferred because they simplify reasoning about the code. Immutable objects are thread-safe and reduce the chance of bugs related to mutable state.

Example of Immutability:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ImmutableExample {
    public static void main(String[] args) {
        List<String> mutableList = new ArrayList<>();
        mutableList.add("Java");
        mutableList.add("Python");

        // Creating an immutable copy of the list
        List<String> immutableList = Collections.unmodifiableList(new ArrayList<>(mutableList));

        // This will throw an UnsupportedOperationException
        immutableList.add("JavaScript");
    }
}

In the above example, immutableList is an immutable copy of mutableList. Attempting to modify the immutable list will result in an UnsupportedOperationException.

First-Class and Higher-Order Functions:

First-class functions treat functions as first-class citizens, allowing them to be passed as arguments to other functions, returned as values from other functions, and assigned to variables.

Higher-order functions are functions that take one or more functions as arguments or return a function as a result.

Example of First-Class and Higher-Order Functions:

import java.util.function.Function;

public class HigherOrderFunctionExample {
    public static void main(String[] args) {
        // Higher-order function taking a function as an argument
        Function<Integer, Integer> square = number -> number * number;

        // First-class functions: passing a function as an argument
        int result = applyFunction(square, 3); // Output: 9
        System.out.println(result);
    }

    // Higher-order function taking a function as an argument
    public static int applyFunction(Function<Integer, Integer> func, int number) {
        return func.apply(number);
    }
}

In the above example, applyFunction is a higher-order function that takes a function as an argument and applies it to a number. The square function is a first-class function passed to applyFunction.

Understanding and applying these functional programming concepts can lead to more robust, maintainable, and parallelizable code, especially in the context of concurrent and distributed systems. Functional programming encourages the use of these concepts, making your code more predictable and easier to reason about.

PreviousMethod ReferencesNextStream API

Last updated 1 year ago