Constructor References in Java

raja
Updated on 10-Jul-2020 13:55:56

329 Views

A constructor reference is just like a method reference except that the name of the method is "new". It can be created by using the "class name" and the keyword "new" with the following syntax.Syntax :: newIn the below example, we are using java.util.function.Function. It is a functional interface whose single abstract method is the apply(). The Function interface represents an operation that takes single argument T and returns a result R.Exampleimport java.util.function.*; @FunctionalInterface interface MyFunctionalInterface {    Employee getEmployee(String name); } class Employee {    private String name;    public Employee(String name) {       this.name = name;    }    public String ... Read More

Implement Listeners Using Lambda Expressions in Java

raja
Updated on 10-Jul-2020 13:50:30

2K+ Views

When we are using a lambda expression for java listener, we do not have to explicitly implement the ActionListener interface. Instead, we can use the below syntax.Syntaxbutton.addActionListener(e -> { // some statements });An ActionListener interface defines only one method actionPerformed(). It is a functional interface which means that there's a place to use lambda expressions to replace the code.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class LambdaListenerTest extends JFrame {    public static void main(String args[]) {       new LambdaListenerTest();    }    private JButton button;    public ClickMeLambdaTest() {       setTitle("Lambda Expression Test");       button = ... Read More

Pass Lambda Expression in a Method in Java

raja
Updated on 10-Jul-2020 12:50:58

3K+ Views

A lambda expression passed in a method that has an argument of type of functional interface. If we need to pass a lambda expression as an argument, the type of parameter receiving the lambda expression argument must be of a functional interface type.In the below example, the lambda expression can be passed in a method which argument's type is "TestInterface". Exampleinterface TestInterface {    boolean test(int a); } class Test {    // lambda expression can be passed as first argument in the check() method    static boolean check(TestInterface ti, int b) {       return ti.test(b);    } } public class ... Read More

Differences Between Lambda Expressions and Closures in Java

raja
Updated on 10-Jul-2020 12:46:52

2K+ Views

Java supports lambda expressions but not the Closures. A lambda expression is an anonymous function and can be defined as a parameter. The Closures are like code fragments or code blocks that can be used without being a method or a class. It means that Closures can access variables not defined in its parameter list and also assign it to a variable.Syntax([comma seperated parameter-list]) -> {body}In the below example, the create() method has a local variable "value" with a short life and disappears when we exit the create() method. This method returns the closure to the caller in the main() method after that ... Read More

Characteristics of Lambda Expressions in Java

raja
Updated on 10-Jul-2020 12:44:50

1K+ Views

The lambda expressions were introduced in Java 8 and facilitate functional programming. A lambda expression works nicely together only with functional interfaces and we cannot use lambda expressions with more than one abstract method.Characteristics of Lambda ExpressionOptional Type Declaration − There is no need to declare the type of a parameter. The compiler inferences the same from the value of the parameter.Optional Parenthesis around Parameter − There is no need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.Optional Curly Braces − There is no need to use curly braces in the expression body if the body contains a ... Read More

Handle Exception Using Lambda Expression in Java

raja
Updated on 10-Jul-2020 12:01:45

3K+ Views

A lambda expression body can't throw any exceptions that haven't specified in a functional interface. If the lambda expression can throw an exception then the "throws" clause of a functional interface must declare the same exception or one of its subtype.Exampleinterface Student {    void studentData(String name) throws Exception; } public class LambdaExceptionTest {    public static void main(String[] args) {       // lamba expression        Student student = name -> {          System.out.println("The Student name is: " + name);          throw new Exception();       };       try {          student.studentData("Adithya");       } catch(Exception e) {       }    } }OutputThe Student name is: Adithya

Use Final or Effectively Final Variable in Lambda Expression in Java

raja
Updated on 10-Jul-2020 11:58:59

2K+ Views

The effectively final variables refer to local variables that are not declared final explicitly and can't be changed once initialized. A lambda expression can use a local variable in outer scopes only if they are effectively final.Syntax(optional) (Arguments) -> bodyIn the below example, the "size" variable is not declared as final but it's effective final because we are not modifying the value of the "size" variable.Exampleinterface Employee {    void empData(String empName); } public class LambdaEffectivelyFinalTest {    public static void main(String[] args) {       int size = 100;       Employee emp = name -> {        // lambda ... Read More

Advantages of Lambda Expressions in Java

raja
Updated on 10-Jul-2020 11:46:57

3K+ Views

A lambda expression is an inline code that implements a functional interface without creating a concrete or anonymous class. A lambda expression is basically an anonymous method.Advantages of Lambda ExpressionFewer Lines of Code − One of the most benefits of a lambda expression is to reduce the amount of code. We know that lambda expressions can be used only with a functional interface. For instance, Runnable is a functional interface, so we can easily apply lambda expressions.Sequential and Parallel execution support by passing behavior as an argument in methods − By using Stream API in Java 8, the functions are passed to collection methods. Now ... Read More

Python Contiguous Boolean Range

Pradeep Elance
Updated on 10-Jul-2020 11:36:51

339 Views

Given a list of values, we are interested to know at which position are the Boolean values present as a contiguous list. Which means after we encounter a value which is TRUE there is a continuous value of true from that position until FALSE value is found. Similarly when a FALSE is found there is a contiguous value of FALSE until TRUE is found.With itertoolsW can use accumulate along with groupby from the itertools module. In this example we take a given list and then apply the accumulate function to keep track of the values that are brought together using ... Read More

Column Deletion from List of Lists in Python

Pradeep Elance
Updated on 10-Jul-2020 11:34:40

1K+ Views

In a list of lists an element at the same index of each of the sublist represents a column like structure. In this article we will see how we can delete a column from a list of lists. Which means we have to delete the element at the same index position from each of the sublist.Using popWe use the pop method which removes the element at a particular position. A for loop is designed to iterate through elements at specific index and removes them using pop.Example Live Demo# List of lists listA = [[3, 9, 5, 1], [4, 6, 1, 2], ... Read More

Advertisements