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.

ImmutableMap.Builder should have remove method

See original GitHub issue

Without 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:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kluevercommented, Sep 6, 2019

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() and Predicate.not(badKey), wrapped in ImmutableMap.copyOf().

0reactions
kluevercommented, Sep 6, 2019

That sounds approximately correct, yes.

Read more comments on GitHub >

github_iconTop 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 >

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