Lambda Example Flashcards

1
Q

Give a clear and concise definition of a lambda expression !

A

A lambda expression is an instantiation of functional interface
A lambda expression can be passed as an argument to a method that has a functional interface as a parameter
A functional interface is an interface that has one abstract method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give the implementation of a class that shows the problem solved by lambda expressions and run it !

A

Here is an example that illustrates the problem that lambdas solve :

We have a class Animal that has 2 boolean fields canHop and canSwim, a constructor that initializes each field and getter methods, we have in the same package a functional interface called CheckTrait with a test method that take an Animal object as a parameter and return a boolean, and we have a class in the same package called CheckHopper that implements the functional interface and provides an implementation of the test
method
which is return a.canHop with a being the animal object that the method takes as an argument,
we have another class in the same package that has a print method that takes as arguments a list of
animals and a functional interface of type CheckTrait, the print method itertaes over the list of animals, checks if the test method of the instance of the object, that implements the functional interface, passed as an argument to the print method returns true in which case the animal is printed otherwise the animal is discarded, in the main method of that same class a list of animals is created, then that list is populated with animal objects, and then the list is passed as an argument to the print method alongside an instance of CheckHopper. If we want to test if an animal is a swimmer we will have to create another class called CheckSwimmer that will have to implement the functional interface and so on. But, if we use a lambda expression and pass it as an argument to the print method each time we want to test if an animal is a swimmer or a hopper we will not have to create classes that implement the functional interface such as CheckHopper and CheckSwimmer.

IMPLEMENTATION :

public class Animal {
private String species;
private boolean canHop;
private boolean canSwim;
public Animal(String speciesName, boolean hopper, boolean swimmer) {
species = speciesName;
canHop = hopper;
canSwim = swimmer;
}
public boolean canHop() { return canHop; }
public boolean canSwim() { return canSwim; }
public String toString() { return species; }
}

public interface CheckTrait {
boolean test(Animal a);
}

public class CheckIfHopper implements CheckTrait {
public boolean test(Animal a) {
return a.canHop();
}
}

1: public class TraditionalSearch {
2: public static void main(String[] args) {
3: List<Animal> animals = new ArrayList<Animal>(); // list of animals
4: animals.add(new Animal("fish", false, true));
5: animals.add(new Animal("kangaroo", true, false));
6: animals.add(new Animal("rabbit", true, false));
7: animals.add(new Animal("turtle", false, true));
8:
9: print(animals, new CheckIfHopper()); // pass class that does check
10: }
11: **private static void print(List<Animal> animals, CheckTrait checker)** {
12: for (Animal animal : animals) {
13: if (checker.test(animal)) // the general check
14: System.out.print(animal + " ");
15: }
16: System.out.println();
17: }
18: }</Animal></Animal></Animal>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give the definition of a functional interface !

A

A functional interface is an interface that has only one abstract method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When does a method accept a lambda expression as an argument ?

A

In Java, a method can accept a lambda expression as an argument when the method’s parameter type is a functional interface, and the functional interface defines a single abstract method whose parameter type matches the parameter type of the lambda expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give an important criteria that is overlooked when passing a lambda expression as an argument to a method !

A

When passing a lambda expression as an argument to a method, a criteria that
is often overlooked is the parameter type of the lambda expression should
match the parameter type of the method in the functional interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Rewrite the class in question 2 with using lambda expression and run it !

A

package TEST;
import java.util.*;
import TEST.Animal;
import TEST.CheckTrait;
import TEST.CheckHopper;
import TEST.CheckSwimmer;
public class TraditionalSearch{

static void **print(List<Animal> animals,CheckTrait check)**{
	for(Animal a:animals){
		if(check.test(a))
			System.out.println(a.toString());
		
		}
	}




**public static void main(String[] args)** {

List<Animal> animals=new ArrayList<Animal>();
animals.add(new Animal("Lion",true,false));
animals.add(new Animal("Shark",false,true));
animals.add(new Animal("Tiger",true,false));
animals.add(new Animal("Dog",true,false));
animals.add(new Animal("Alligator",false,true));

print(animals,new CheckHopper());
System.out.println("-------------------------------");
print(animals,new CheckSwimmer());
System.out.println();
System.out.println();
**System.out.println("Using Lambda expression for hoppers");**
print(animals,a->a.canHop());
**System.out.println("Using Lambda expression for swimmers");**
print(animals,a->a.canSwim());
	
	}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Give the definition of deferred execution !

A

Deferred execution typically involves delaying the execution of a piece of code, such as a method call or a block of code, until it is explicitly triggered or needed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Give an example of deferred execution in the class implemented in question 6 !

A

In the provided class TraditionalSearch, the only deferred execution occurs with the lambda expressions used as arguments to the print method calls.
The lambda expressions themselves are not executed immediately when they are defined but are deferred until the print method processes each Animal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly