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 Interface

Definition and Purpose:

Functional Interface in Java is an interface that contains only one abstract method. Functional interfaces are used to provide target types for lambda expressions and method references. They enable functional programming features in Java, allowing developers to write more concise and expressive code.

The primary purpose of functional interfaces is to enable the use of lambda expressions and method references. Lambda expressions can be used to provide implementations for the single abstract method of functional interfaces, making code more readable and maintainable.

java.util.function Package:

The java.util.function package in Java provides a collection of functional interfaces that represent different types of operations that can be performed on data. These interfaces are designed to be used with lambda expressions and method references.

Built-in Functional Interfaces:

  1. Predicate<T>:

    • Represents a predicate (boolean-valued function) of one argument.

    • Example:

      Predicate<Integer> isEven = (num) -> num % 2 == 0;
      System.out.println(isEven.test(4)); // Output: true
  2. Consumer<T>:

    • Represents an operation that takes a single input argument and returns no result.

    • Example:

      Consumer<String> printUpperCase = (str) -> System.out.println(str.toUpperCase());
      printUpperCase.accept("hello"); // Output: HELLO
  3. Function<T, R>:

    • Represents a function that takes one argument and produces a result.

    • Example:

      Function<String, Integer> strLength = (str) -> str.length();
      System.out.println(strLength.apply("hello")); // Output: 5
  4. Supplier<T>:

    • Represents a supplier of results.

    • Example:

      Supplier<Double> randomValue = () -> Math.random();
      System.out.println(randomValue.get()); // Output: a random double value
  5. UnaryOperator<T>:

    • Represents an operation on a single operand that produces a result of the same type as its operand.

    • Example:

      UnaryOperator<Integer> square = (num) -> num * num;
      System.out.println(square.apply(5)); // Output: 25
  6. BinaryOperator<T>:

    • Represents an operation upon two operands of the same type, producing a result of the same type as the operands.

    • Example:

      BinaryOperator<Integer> sum = (a, b) -> a + b;
      System.out.println(sum.apply(2, 3)); // Output: 5

These functional interfaces allow developers to pass behavior in a more flexible way, making it easier to write generic and reusable code in Java. They can be used in various contexts, including collections, streams, and concurrent programming. Functional interfaces, along with lambda expressions, form the foundation of functional programming in Java.

PreviousIntroduction to Lambda ExpressionsNextFiltering, Mapping, Reducing

Last updated 1 year ago