Found 4338 Articles for Java 8

How to use a return statement in lambda expression in Java?

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

11K+ Views

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 More

How to implement an action listener using method reference in Java?

raja
Updated on 11-Jul-2020 07:11:00

503 Views

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");       button = new JButton("Method Reference");       ... Read More

Differences between Lambda Expression and Method Reference in Java?

raja
Updated on 11-Jul-2020 06:54:44

6K+ Views

Lambda expression is an anonymous method (method without a name) that has used to provide the inline implementation of a method defined by the functional interface while a method reference is similar to a lambda expression that refers a method without executing it. The arrow (->) operator can be used to connect the argument and functionality in a lambda expression while the (::) operator separates the method name from the name of an object or class in a method reference.Syntax for Lambda Expression([comma seperated argument-list]) -> {body}Syntax for Method Reference :: Exampleimport java.util.*; public class LambdaMethodReferenceTest {    public static void main(String args[]) { ... Read More

How to implement an instance method of an arbitrary object using method reference in Java?

raja
Updated on 11-Jul-2020 06:20:53

664 Views

A method reference is a new feature in Java 8, which is related to Lambda Expression. It can allow us to reference constructors or methods without executing them. The method references and lambda expressions are similar in that they both require a target type that consists of a compatible functional interface.SyntaxClass :: instanceMethodNameExampleimport java.util.*; import java.util.function.*; public class ArbitraryObjectMethodRefTest {    public static void main(String[] args) {       List persons = new ArrayList();       persons.add(new Person("Raja", 30));       persons.add(new Person("Jai", 25));       persons.add(new Person("Adithya", 20));       persons.add(new Person("Surya", 35));       persons.add(new ... Read More

How can we iterate the elements of List and Map using lambda expression in Java?

raja
Updated on 11-Jul-2020 06:15:14

10K+ Views

The lambda expressions are inline code that implements a functional interface without creating an anonymous class. In Java 8, forEach statement can be used along with lambda expression that reduces the looping through a Map to a single statement and also iterates over the elements of a list. The forEach() method defined in an Iterable interface and accepts lambda expression as a parameter.Example ( List using Lambda Expression)import java.util.*; public class ListIterateLambdaTest {    public static void main(String[] argv) {       List countryNames = new ArrayList();       countryNames.add("India");       countryNames.add("England");       countryNames.add("Australia");     ... Read More

Convert an object to another type using map() method with Lambda in Java?

raja
Updated on 11-Jul-2020 06:13:04

8K+ Views

In Java 8, we have the ability to convert an object to another type using a map() method of Stream object with a lambda expression. The map() method is an intermediate operation in a stream object, so we need a terminal method to complete the stream.SyntaxStream map(Function

Type Inference in Lambda expression in Java?

raja
Updated on 11-Jul-2020 06:05:19

846 Views

Type Inference states that the data type of any expression. For instance, a method return type or argument type can be understood automatically by the compiler. Types in the argument list can be omitted since java already know the types of the expected parameters for the single abstract method of functional interface.Syntax(var1, var2 …) -> { method body }In the below example, we can sort a String[] array by its last character.Exampleimport java.util.Arrays; public class TypeInferencingLambdaTest {    public static void main(String[] args) {       String[] names = {"Raja", "Jai", "Adithya", "Surya", "Chaitanya", "Ravi", "Krishna"};       Arrays.sort(names, (s1, ... Read More

Is it mandatory to mark a functional interface with @FunctionalInterface annotation in Java?

raja
Updated on 10-Jul-2020 14:11:42

2K+ Views

An interface defined with only one abstract method is known as Functional Interface. It's not mandatory to mark the functional interface with @FunctionalInterface annotation, the compiler doesn't throw any error. But it’s good practice to use @FunctionalInterface annotation to avoid the addition of extra methods accidentally. If an interface annotated with @FunctionalInterface annotation and more than one abstract method then it throws a compile-time error.Syntax@FunctionalInterface interface interface_name {    // only one abstarct method declaration }Example@FunctionalInterface interface Shape {    void printArea(int x); } public class SquareTest {    public static void main (String args[]) {       Shape square ... Read More

How to debug lambda expressions in Java?

raja
Updated on 10-Jul-2020 14:09:46

2K+ Views

The lambda expression composed of two parts, one is an argument and another one is code or expression. These two parts have separated by an arrow operator "->". We can use different IDE's like Netbeans, IntelliJ,  and Eclipse to debug the lambda expressions in Java. It is always possible to create multi-line lambda expressions and use print statements to display the values of a variable. The debugger can also provide additional information about the state of a java program. It allows some variables to be modified while the debugger is executing.Syntax(parameters) -> expression   or (parameters) -> { statements; }Exampleimport java.util.*; public class LambdaDebugTest { ... Read More

How can we write a multiline lambda expression in Java?

raja
Updated on 10-Jul-2020 14:08:21

9K+ Views

Lambda expression is an anonymous method that has used to provide an implementation of a method defined by a functional interface. In Java 8, it is also possible for the body of a lambda expression to be a complex expression or statement, which means a lambda expression consisting of more than one line. In that case, the semicolons are necessary. If the lambda expression returns a result then the return keyword is also required.Syntax([comma seperated argument-list]) -> { multiline statements }Exampleinterface Employee {    String displayName(String s); } public class MultilineLambdaTest {    public static void main(String[] s) {       ... Read More

Advertisements