Javatpoint Logo
Javatpoint Logo

Higher Order functions in Java

In the world of programming, higher-order functions are a powerful and versatile concept. They allow you to treat functions as first-class citizens, enabling you to pass functions as arguments, return functions from other functions, and even store functions in data structures. With the introduction of functional programming features in Java 8, such as lambdas and the java.util.function package, it's now possible to implement higher-order functions in Java. In this section, we'll explore what higher-order functions are, how to create them in Java, and provide detailed examples to illustrate their use.

What Are Higher-Order Functions?

Higher-order functions are functions that operate on other functions. In simpler terms, they take one or more functions as arguments and/or return a function as a result. Higher-order functions are a fundamental concept in functional programming and enable code that is more concise, modular, and expressive.

Functional Interfaces in Java

Java introduced functional interfaces in Java 8, which are interfaces with a single abstract method (SAM), also known as functional methods. These functional interfaces serve as the foundation for creating higher-order functions in Java.

Higher-Order Functions

1. forEach:

forEach is used to perform an action on each element of a collection.

2. map:

map transforms each element of a collection and returns a new array with the transformed elements.

3. compactMap:

4. flatMap:

flatMap is used when you want to flatten nested arrays or collections.

5. filter:

filter is used to create a new array containing elements that satisfy a given condition.

6. reduce:

reduce combines all elements in a collection into a single value using a given operation.

7. sort and sorted:

  • sort is used to sort an array in-place.
  • sorted returns a new sorted array without modifying the original.

These higher-order functions in Swift make it easy to work with collections and apply transformations or filtering operations, promoting functional and concise code.

Here are some commonly used functional interfaces from the java.util.function package:

  • Function<T, R>: Represents a function that takes an argument of type T and returns a result of type R.
  • Predicate<T>: Represents a function that takes an argument of type T and returns a boolean.
  • Consumer<T>: Represents a function that takes an argument of type T and performs an action without returning a result.
  • Supplier<T>: Represents a function that takes no arguments and returns a value of type T.

Creating Higher-Order Functions in Java

Let's dive into examples of higher-order functions in Java. We'll explore scenarios where we pass functions as arguments and return functions as results.

1. Passing Functions as Arguments

Suppose we want to create a higher-order function that applies a given function to each element of a list and returns a new list with the results. We can achieve this using the Function functional interface.

HigherOrderFunctionsExample.java

Output:

[2, 4, 6, 8, 10]

In this example, the map function accepts a list and a function as arguments. It uses the provided function to transform each element of the list and returns a new list with the transformed values.

2. Returning Functions as Results

Now, let's create a higher-order function that generates and returns a function based on some criteria. In this example, we'll implement a simple calculator that can perform addition, subtraction, multiplication, or division.

HigherOrderFunctionsExample.java

Output:

Addition: 15
Subtraction: 5

In this example, the calculator function takes an operation as a parameter and returns a corresponding binary operator function. Depending on the operation, the returned function will perform addition, subtraction, multiplication, or division.

Conclusion

Higher-order functions are a valuable tool in Java that allows us to write more modular and expressive code. With the support for functional interfaces, lambdas, and method references introduced in Java 8, we can create higher-order functions to pass functions as arguments and return functions as results, enabling more flexibility in code. By understanding and utilizing higher-order functions, we can leverage the full power of functional programming paradigms in Java, leading to cleaner, more concise, and more maintainable code.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA