Javatpoint Logo
Javatpoint Logo

Anonymous Function in Java

In Java, anonymous functions, also known as lambda expressions. It introduced in Java 8 as a way to provide more concise and readable code. They allow us to define a function in a single line of code without having to explicitly define a class or interface.

What is an Anonymous Function?

An anonymous function is a function that has no name and is not bound to an identifier. It is typically defined inline and can be passed as an argument to another function or returned as a value from a function. In Java, anonymous functions are implemented as lambda expressions, which are a shorthand way of defining an anonymous function.

Syntax

The syntax for an anonymous function in Java is as follows:

The parameters are the inputs to the function, and the body is the code that executes when the function is called. Here is an example of an anonymous function that takes two integers as inputs and returns their sum:

The above anonymous function can be passed as an argument to another function or assigned to a variable:

In this example, we define a variable called add that is a function that takes two integers as inputs and returns their sum.

Advantages of Anonymous Functions

The use of anonymous functions in Java has several advantages, including:

Concise syntax: Anonymous functions provide a concise way of defining a function without having to define a separate class or interface.

Readable code: Because anonymous functions are defined inline, the code can be easier to read and understand.

Flexibility: Anonymous functions can be passed as arguments to other functions or returned as values from functions, providing more flexibility in how functions are used.

Improved performance: The use of anonymous functions can improve performance by reducing the amount of memory and processing power required to define and call functions.

here's an example Java code that demonstrates the use of anonymous functions (lambda expressions) to filter a list of integers:

AnonymousFunctionExample.java

Output:

Even numbers:
2
4

In this code, we define a list of integers and then use an anonymous function (lambda expression) to define a predicate that filters the list to only include even numbers. We then use the filter method to apply the predicate to the list and return a new list of even numbers. Finally, we print out the even numbers using a for loop.

Here's another example Java code that demonstrates the use of anonymous functions (lambda expressions) to sort a list of strings:

AnonymousFunctionExample.java

Output:

Sorted list of fruits:
Apple
Banana
Grape
Orange
Pineapple

In the above code, we have defined a list of fruits and then use an anonymous function (lambda expression) to define a comparator that sorts the list alphabetically. We then use the sort method to apply the comparator to the list and sort it. Finally, we print out the sorted list of fruits using a for loop.

Conclusion

Anonymous functions in Java provide a concise and readable way of defining functions without having to define a separate class or interface. They can be used to improve the performance of code and provide more flexibility in how functions are used. With the introduction of lambda expressions in Java 8, anonymous functions have become an important tool for developers to write more efficient and 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