https://github.com/talsma-ict/lazy4j
Lazy value support for java
https://github.com/talsma-ict/lazy4j
lazy lazy-evaluation lazy-loading lazyloading
Last synced: 5 months ago
JSON representation
Lazy value support for java
- Host: GitHub
- URL: https://github.com/talsma-ict/lazy4j
- Owner: talsma-ict
- License: apache-2.0
- Created: 2018-05-03T08:36:08.000Z (about 8 years ago)
- Default Branch: main
- Last Pushed: 2025-12-22T07:25:00.000Z (6 months ago)
- Last Synced: 2025-12-23T18:53:24.056Z (6 months ago)
- Topics: lazy, lazy-evaluation, lazy-loading, lazyloading
- Language: Java
- Homepage:
- Size: 600 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[![Maven Version][maven-img]][maven]
[![JavaDoc][javadoc-img]][javadoc]
[](https://sonarcloud.io/summary/new_code?id=talsma-ict_lazy4j)
[![Coverage Status][coveralls-img]][coveralls]
# Lazy4J
- A `Lazy` class in java.
- A `LazyValueMap`.
- A `LazyList`
## What is it?
### Lazy
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.
### LazyValueMap
Map that stores values as `Lazy` values.
### LazyList
List that stores values as `Lazy` values.
## Why?
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.
## Example
A small example of how the `Lazy` class can be used:
```java
public class Example {
// Method reference to constructor new Expensive(), called only when needed and keep the result.
private final Lazy lazyMethod = Lazy.of(Expensive::new);
// Lambda called only once when needed for the first time.
private final Lazy 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][lazy-javadoc-page] for full descriptions.
### LazyValueMap example
```java
// Map that stores lazy values that only get evaluated when they are needed for the first time.
private final LazyValueMap lazyMap = new LazyValueMap(HashMap::new);
private final LazyValueMap 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`
### LazyList example
```java
// List that stores lazy values that only get evaluated when they are needed for the first time.
LazyList 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.
## Getting the classes
Add the following dependency to your project
or download it [directly from github](https://github.com/talsma-ict/lazy4j/releases):
#### Maven
```xml
nl.talsmasoftware
lazy4j
[see maven badge]
```
#### Gradle
```groovy
compile 'nl.talsmasoftware:lazy4j:[see maven-central badge]'
```
#### Scala
```scala
libraryDependencies += "nl.talsmasoftware" % "lazy4j" % "[see maven-central badge]"
```
## License
[Apache 2.0 license](LICENSE)
[maven-img]:
[maven]:
[coveralls-img]:
[coveralls]:
[javadoc-img]:
[javadoc]:
[lazy-javadoc-page]: