if Condition in Lambda Expression JavaLambda expressions are one of the most powerful features introduced in Java 8. They are a concise way to express functions and can make your code much more readable and maintainable. One of the lesserknown features of lambda expressions is the ability to use conditional statements. In this article, we will explore how to use if conditions in lambda expressions in Java. What are lambda expressions?Lambda expressions are a way to define a function inline, without the need for a separate method. They are essentially a shorthand for writing anonymous inner classes. Lambda expressions have the following syntax: The parameters are a commaseparated list of the input parameters to the function, and the body is the code that is executed when the function is called. The > operator separates the parameter list from the body. For example, let's say we want to define a function that takes two integers and returns their sum. We could do this using a lambda expression as follows: This lambda expression takes two integers as input (int a and int b) and returns their sum (return a + b;). Using if conditions in lambda expressionsLet's look at how we can use if conditions in them. We can use the ternary operator (?:) to create a conditional expression within the body of a lambda expression. The ternary operator takes three operands: a boolean condition, an expression to be evaluated if the condition is true, and an expression to be evaluated if the condition is false. For example, let's say we want to define a function that takes an integer and returns "positive" if it is greater than zero, "negative" if it is less than zero, and "zero" if it is equal to zero. We can do this using a lambda expression with a conditional expression as follows: This lambda expression takes an integer as input (int n) and returns a string based on its value. If n is greater than zero, it returns "positive". If n is less than zero, it returns "negative". If n is equal to zero, it returns "zero". Let's look at a complete example of using if conditions in lambda expressions. Suppose we have a list of integers and we want to filter out the negative ones and print the positive ones. We can use a lambda expression with a conditional expression to achieve this as follows: LambdaIfExample.java In this example, we create a list of integers using the Arrays.asList() method. We then use the stream() method to create a stream of the integers, which allows us to apply lambda expressions to each element of the list.We use the filter() method to filter out the negative numbers (n > n >= 0), and then we use the forEach() method to apply a lambda expression to each remaining positive or zero number. The lambda expression in the forEach() method uses a conditional expression to print out whether the number is positive or zero. If the number is greater than zero (n > 0), it prints out a message saying it is positive. Otherwise, if the number is zero, it prints out a message saying it is zero. Output: 2 is positive 4 is positive 6 is positive 8 is positive 10 is positive 0 is zero As we can see, the negative numbers have been filtered out, and the positive and zero numbers have been printed with the appropriate message. ConclusionIn this article, we have seen how to use if conditions in lambda expressions in Java. We can use the ternary operator (?:) to create a conditional expression within the body of a lambda expression. This allows us to write concise and expressive code that can make our programs more readable and maintainable. Lambda expressions are a powerful feature of Java 8, and mastering them can make you a more productive and effective Java developer. By using if conditions in lambda expressions, you can write even more sophisticated and complex functions that can handle a wide variety of use cases. We hope this article has been helpful in understanding how to use if conditions in lambda expressions in Java. Happy coding!
Next TopicChained Exceptions in Java
