- A
Lazy
class in java. - A
LazyValueMap
. - A
LazyList
Wrapper for a standard java Supplier
function that only evaluates the wrapped function when it is first needed,
remembering the result so it does not need to get called again.
Map that stores values as Lazy
values.
List that stores values as Lazy
values.
We feel this ought to be provided out of the box and should have been when lambda's were introduced, back in Java 8.
Fortunately, a Lazy
class implementation is not very difficult to create, so that's what we did.
The LazyValueMap
was added later in version 2.0.2 and LazyList
in 2.0.3.
A small example of how the Lazy
class can be used:
public class Example {
// Method reference to constructor new Expensive(), called only when needed and keep the result.
private final Lazy<Expensive> lazyMethod = Lazy.of(Expensive::new);
// Lambda called only once when needed for the first time.
private final Lazy<Expensive> lazyLambda = Lazy.of(() -> new Expensive());
}
This declares a lazy variable without calling the expensive supplier yet.
Only when get()
is called for the first time, the new Expensive()
constructor is called.
All subsequent invocations will return the same instance of Expensive
.
Lazy provides the following methods:
isAvailable
returning whether the lazy value is already available.map
applies a function on the lazy result.flatMap
applies a function that itself returns a supplier.ifAvailable
runs a function only if the lazy value is already available.
Please refer to the Lazy class documentation for full descriptions.
// Map that stores lazy values that only get evaluated when they are needed for the first time.
private final LazyValueMap<String, String> lazyMap = new LazyValueMap(HashMap::new);
private final LazyValueMap<String, String> sortedLazyMap = new LazyValueMap(TreeMap::new);
lazyMap.putLazy("key", () -> new Expensive());
The expensive value is only evaluated when the key is first accessed.
The lazy map offers the following lazy variants of standard map methods:
getLazy
putLazy
lazyPutIfAbsent
lazyCompute
lazyComputeIfAbsent
lazyComputeIfPresent
lazyReplace
lazyMerge
lazyValues
// List that stores lazy values that only get evaluated when they are needed for the first time.
LazyList<Expensive> lazyValues = LazyList.create();
lazyValues.addLazy(() -> new Expensive("first"));
lazyValues.addLazy(() -> new Expensive("second"));
The expensive values are only evaluated when the values are first accessed.
The lazy list offers the following lazy variants of standard list methods:
- getLazy / getFirstLazy / getLastLazy
- setLazy
- addLazy / addFirstLazy / addLastLazy / addAllLazy
- streamLazy / streamAvailable / parallelStreamLazy / parallelStreamAvailable
- replaceAll is performed lazily per value
Please be aware that certain operations such as sort
or hashCode
will trigger eager evaluation of all values in the list.
Add the following dependency to your project or download it directly from github:
<dependency>
<groupId>nl.talsmasoftware</groupId>
<artifactId>lazy4j</artifactId>
<version>[see maven badge]</version>
</dependency>
compile 'nl.talsmasoftware:lazy4j:[see maven-central badge]'
libraryDependencies += "nl.talsmasoftware" % "lazy4j" % "[see maven-central badge]"