Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Java Articles
Page 286 of 450
Importance of Predicate interface in lambda expression in Java?
Predicate is a generic functional interface that represents a single argument function that returns a boolean value (true or false). This interface available in java.util.function package and contains a test(T t) method that evaluates the predicate of a given argument.Syntaxpublic interface Predicate { boolean test(T t); }Exampleimport java.util.*; import java.util.functionPredicate; public class LambdaPredicateTest { public static void main(String args[]) { Employee emp1 = new Employee("Raja", 26); Employee emp2 = new Employee("Jaidev", 24); Employee emp3 = new Employee("Adithya", ...
Read MoreHow to use method references with Generics in Java?
The method references are introduced in Java 8 similar to lambda expression. It can allow us to reference methods or constructors without executing them. The method references and lambda expressions require a target type that consists of a compatible functional interface. We can also use a method reference with generic classes and generic methods in java.Exampleinterface MyFunc { int func(T[] vals, T v); } class MyArrayOps { static int countMatching(T[] vals, T v) { int count = 0; for(int i=0; i < ...
Read MoreWhat are the rules for a local variable in lambda expression in Java?
A lambda expression can capture variables like local and anonymous classes. In other words, they have the same access to local variables of the enclosing scope. We need to follow some rules in case of local variables in lambda expressions.A lambda expression can't define any new scope as an anonymous inner class does, so we can't declare a local variable with the same which is already declared in the enclosing scope of a lambda expression.Inside lambda expression, we can't assign any value to some local variable declared outside the lambda expression. Because the local variables declared outside the lambda expression ...
Read MoreHow to use an ArrayList in lambda expression in Java?
The lambda expression is an inline code that can implement a functional interface without creating an anonymous class. An ArrayList can be used to store a dynamically sized collection of elements.In the below program, We have removed the elements of an ArrayList whose age is less than or equal to 20 using the removeIf() method. This method introduced in Java 8 version to remove all elements from a collection that satisfy a condition.Syntaxpublic boolean removeIf(Predicate filter) The parameter filter is a Predicate. If the given predicate satisfies the condition, the element can be removed. This method returns the boolean value ...
Read MoreHow can we use this and super keywords in method reference in Java?
The method reference is similar to a lambda expression that refers to a method without executing it and "::" operator can be used to separate a method name from the name of an object or class in a method reference.The methods can be referenced with the help of this and super keywords in Java. The super keyword can be used as a qualifier to invoke the overridden method in a class or an interface.syntaxthis::instanceMethod TypeName.super::instanceMethodExampleimport java.util.function.Function; interface Defaults { default int doMath(int a) { return 2 * a; ...
Read MoreWhat is a target type of lambda expression in Java?
A functional Interface for which a lambda expression has invoked is called target type of lambda expression. It means that if a lambda expression has invoked for some "X" interface then "X" is the target type of that lambda expression. Hence, we conclude that lambda expressions can be used only in those situations where java compiler can determine the Target Type.In the below example, the target type of lambda expression is BiFunction. An instance of a class is automatically created that implements the functional interface and lambda expression provides an implementation of the abstract method declared by the functional interface.Exampleinterface BiFunction { ...
Read MoreHow to write a conditional expression in lambda expression in Java?
The conditional operator is used to make conditional expressions in Java. It is also called a Ternary operator because it has three operands such as boolean condition, first expression, and second expression.We can also write a conditional expression in lambda expression in the below program.Exampleinterface Algebra { int substraction(int a, int b); } public class ConditionalExpressionLambdaTest { public static void main(String args[]) { System.out.println("The value is: " + getAlgebra(false).substraction(20, 40)); System.out.println("The value is: " + getAlgebra(true).substraction(40, 10)); } static Algebra getAlgebra(boolean reverse) { Algebra alg = reverse ...
Read MoreHow to use a return statement in lambda expression in Java?
A return statement is not an expression in a lambda expression. We must enclose statements in braces ({}). However, we do not have to enclose a void method invocation in braces. The return type of a method in which lambda expression used in a return statement must be a functional interface.Example 1public class LambdaReturnTest1 { interface Addition { int add(int a, int b); } public static Addition getAddition() { return (a, b) -> a + b; // lambda expression return statement } public static void main(String args[]) { System.out.println("The ...
Read MoreHow to initialize an array using lambda expression in Java?
An array is a fixed size element of the same type. The lambda expressions can also be used to initialize arrays in Java. But generic array initializes cannot be used.Example-1interface Algebra { int operate(int a, int b); } public class LambdaWithArray1 { public static void main(String[] args) { // Initialization of an array in Lambda Expression Algebra alg[] = new Algebra[] { ...
Read MoreHow to implement an action listener using method reference in Java?
In Java 8, lambda expression accepts an anonymous function as a parameter. In the case of providing an anonymous method, we can also pass references of existing methods using "::" symbol. A method reference enables us to do the same thing but with existing methods.We can also implement an action listener for a JButton by using static method reference and referred using the class name.Syntax:: Method-name;Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class MethodReferenceButtonListenerTest extends JFrame { private JButton button; public MethodReferenceButtonListenerTest() { setTitle("Method Reference Button Listener"); ...
Read More