ImmutableMap.Builder should have remove method
See original GitHub issueWithout Builder.remove()
method, it will be very inefficient to create a new immutable map from an existing immutable map removing a key:
Map newMapWithoutKey1 = ImmutableMap.builder()
.putAll(map)
.remove("key1")
.build();
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Efficiently "modifying" an ImmutableMap - Stack Overflow
We're currently using Guava for its immutable collections but I was surprised to find that their maps don't have methods to easily create...
Read more >Immutable Map Implementations in Java - Baeldung
It is sometimes preferable to disallow modifications to the java.util.Map such as sharing read-only data across threads.
Read more >ImmutableMap (Guava: Google Core Libraries for Java 23.0 API)
Returns an immutable map containing a single entry. This map behaves and performs comparably to Collections.singletonMap(K, V) but will not accept a null...
Read more >Immutable Map in Java - GeeksforGeeks
ImmutableMap, as suggested by the name, is a type of Map which is immutable. · If any attempt made to add, delete and...
Read more >Immutable Map in Java (using Guava and Java 9)
This post will discuss various methods to create an immutable map in Java. Immutable Map do not support any modification operations such as...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That seems fine to me.
If you’re super worried about performance, you can add the entries from the existing mutable map to the
ImmutableMap
builder in a loop, and just don’t add the entry if it has a certain key (the key you want to “remove”).Or, alternatively, use a lazy
Maps.filterKeys()
andPredicate.not(badKey)
, wrapped inImmutableMap.copyOf()
.That sounds approximately correct, yes.