What are the lazy and eager operators?

When studying the Stream API, I often encountered the use of the expressions lazy and eager operators, but I could not really find a normal definition of these concepts anywhere.

Can someone explain clearly?

Author: Vennic, 2019-02-19

1 answers

In programming terms:

  • Lazy evaluation means that calculations are performed only when the result is needed.
  • Eager evaluation - means that calculations are performed strictly on request.

For example, let's say you sort the array int arr[] - the operation is quite computationally expensive.

Traditionally, it is customary to choose the eager approach, call the array sorting: Arrays.sort(arr), then use the result.

In the case of lazy - the array is not sorted in advance, but we sort only at the moment when we need arr[i] - a certain element of the sorted array - and only then we call sorting-do you feel the difference?

In the Stream API, almost all operations are lazy, intermediate calculations are performed only when calling terminal operations of the type reduce()/count()/collect() , etc.

 3
Author: Barmaley, 2019-02-19 07:18:16