Found 4338 Articles for Java 8

What is the generic functional interface in Java?

raja
Updated on 11-Jul-2020 12:23:09

3K+ Views

A lambda expression can't specify type parameters, so it's not generic. However, a functional interface associated with lambda expression is generic. In this case, the target type of lambda expression has determined by the type of argument(s) specified when a functional interface reference is declared.Syntaxinterface SomeFunc {  T func(T t); }Exampleinterface MyGeneric {    T compute(T t); } public class LambdaGenericFuncInterfaceTest {    public static void main(String args[]) {       MyGeneric reverse = (str) -> {   // Lambda Expression          String result = "";          for(int i = str.length()-1; i >= 0; i--)           ... Read More

What are the rules for a local variable in lambda expression in Java?

raja
Updated on 11-Jul-2020 11:57:48

3K+ Views

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 can be final or effectively final.The rule of ... Read More

How to use an ArrayList in lambda expression in Java?

raja
Updated on 11-Jul-2020 11:40:27

2K+ Views

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 true if elements are removed, false otherwise.Exampleimport java.util.*; ... Read More

How can we use this and super keywords in method reference in Java?

raja
Updated on 11-Jul-2020 08:55:49

860 Views

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;    } } public class Calculator implements Defaults { ... Read More

What is a target type of lambda expression in Java?

raja
Updated on 11-Jul-2020 08:30:22

2K+ Views

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 More

What are the rules to follow while using exceptions in java lambda expressions?

raja
Updated on 20-Dec-2019 08:36:16

3K+ Views

The lambda expressions can't be executed on their own. It is used to implement methods that have declared in a functional interface. We need to follow a few rules in order to use the exception handling mechanism in a lambda expression.Rules for Lambda ExpressionA lambda expression cannot throw any checked exception until its corresponding functional interface declares a throws clause.An exception thrown by any lambda expression can be of the same type or sub-type of the exception declared in the throws clause of its functional interface.Example-1interface ThrowException { void throwing(String message); } public class LambdaExceptionTest1 { ... Read More

How to pass a lambda expression as a method parameter in Java?

raja
Updated on 19-Dec-2019 13:09:20

6K+ Views

A lambda expression is an anonymous or unnamed method in Java. It doesn't execute on its own and used to implement methods that are declared in a functional interface. If we want to pass a lambda expression as a method parameter in java, the type of method parameter that receives must be of functional interface type.Exampleinterface Algebra { int operate(int a, int b); } enum Operation { ADD, SUB, MUL, DIV } public class LambdaMethodArgTest { public static void main(String[] args) { print((a, b) -> ... Read More

How to write a conditional expression in lambda expression in Java?

raja
Updated on 11-Jul-2020 07:48:12

3K+ Views

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 More

What is a casting expression in Java?

raja
Updated on 11-Jul-2020 07:40:37

589 Views

A cast expression provides a mechanism to explicitly provide a lambda expression's type if none can be conveniently inferred from context. It is also useful to resolve ambiguity when a method declaration is overloaded with unrelated functional interface types.SyntaxObject o = () -> { System.out.println("TutorialsPoint"); }; // Illegal: Object o = (Runnable) () -> { System.out.println("TutorialsPoint"); }; // LegalExampleinterface Algebra1 {    int operate(int a, int b); } interface Algebra2 {    int operate(int a, int b); } public class LambdaCastingTest {    public static void main(String[] args) {       printResult((Algebra1)(a, b) -> a + b);  // Cast Expression ... Read More

How to initialize an array using lambda expression in Java?

raja
Updated on 11-Jul-2020 07:31:06

2K+ Views

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[] {                                         (a, b) -> a+b,             ... Read More

Advertisements