question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an immutable computing map

See original GitHub issue

(Sorry, hit post by mistake, and the initial post was empty. Edited afterwards.)

Please consider providing an ImmutableMap that works by computing only once the value associated to each key, lazily, and storing the result. Concretely, I propose to add: Maps#immutableComputingMap(Set<K> keys, Function<K, V> valueProvider): ImmutableMap<K, V>. The returned map has a constant keySet(), the one given by the keys parameter at creation time (defensively copied, of course). The returned map behaves as follows: if the value corresponding to some key was never computed yet, compute it using the given function and store the result when needed (lazily); if it ever was, return that previously computed result.

The returned map is indeed immutable in the sense that all the calls on the map are idempotent: calling a given method with given parameters twice always returns the same result.

Use case: a class provides a getSomeData(): ImmutableMap<K, V> (for some K, V) under the form of a map, that is (in some cases) slow to compute. Initially the whole map is computed at once when getSomeData() is called. The designer of the class later wants to change it, upon realizing that in some use cases only a small subset of the keys is used by the callers of that method. Thus, the designer wants to compute the values lazily, only when get(k) is called on the returned map.

This idea is not to be mixed up with similar ideas that have been rejected because they do not satisfy the map contract. This proposal does satisfy the map contract, as far as I can see.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
perceptron8commented, Feb 5, 2021

@oliviercailloux If ImmutableMap involved lazy computing, I wouldn’t trust any ImmutableMap returned by any API ever again. “Is it safe to pass on?”, “Should I iterate over values() to trigger loading first?” and so on. I know that it is not specified directly to be so, but most of the times I treat “immutable” as “set in stone”.

It is pretty easy to get computing map already using guava cache or caffeine like this:

public static <K, V> Map<K, V> toComputingMap(Set<K> set, Function<? super K, V> function) {
	CacheLoader<? super K, V> loader = CacheLoader.from(function);
	LoadingCache<? super K, V> cache = CacheBuilder.newBuilder().build(loader);
	return Maps.asMap(set, cache::getUnchecked);
}

You can make it more immutable-like by taking ImmutableSet<K> instead of Set<K>.

1reaction
ben-manescommented, Feb 5, 2021

In the description, a call like size() or keySet() would change over time. It would therefore not be immutable, but would be not directly modifiable. Instead it seems like @perceptron8 sample, wrapped with Collections.unmodifiableMap, would serve this purpose and require no library changes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Immutable Map in Java - GeeksforGeeks
ImmutableMap, as suggested by the name, is a type of Map which is immutable. It means that the content of the map are...
Read more >
How to create an Immutable List, Set, and Map in Java?
Hello guys, today, I am going to share bout my favorite Java 9 feature “factory methods for collection”, which is introduced as part...
Read more >
How to create immutable Map in Java - Java Code Geeks - 2022
Interested to learn about immutabe Map? Check our article explaining how we can create an immutable Map in Java with examples.
Read more >
java - Extending an ImmutableMap with additional or new values
This can be done by going through an initializer block of a different Map (e.g., ... Just copy the ImmutableMap into a new...
Read more >
Immutable Map Implementations in Java - Baeldung
An Immutable Map, on the other hand, contains its own private data and doesn't allow modifications to it. Therefore, the data cannot change...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found