Lazy evaluation is an evaluation strategy which holds the evaluation of an expression until its value is needed.
It avoids repeated evaluation.
Haskell is a good example of such a functional programming language whose fundamentals are based on Lazy Evaluation.
xxxxxxxxxx
package com.basicsstrong.apidesign;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Parctice02 {
public static void main(String[] args) {
List<Person> persons = new ArrayList<>();
persons.add(new Person("John",36));
persons.add(new Person("Erick",39));
persons.add(new Person("Mickel",56));
persons.add(new Person("Bob",33));
// This is just a Intermediate Operation Let see if the get Method gets executed by
// Putting a Debug code there
Stream<Person> stream = persons.stream()
.filter(e -> e.getAge() <= 36);
// Nothing Happenes.
// Let's now add one terminal operation
List<Person> newlist = persons.stream()
.filter(e -> e.getAge() <= 36)
.collect(Collectors.toList());
// See the getMethod call takes place and Stream is filtered
System.out.println(newlist);
}
}
class Person{
String name;
int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
System.out.println("I am in getAge Mthod of Persons");
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}