Skip to content
Advertisement

How to use replaceValues method of Guava Multimap?

I want to add, remove and replace values in a MultiMap provided by Guava.

I do this currently to add values..

JavaScript

Removing values is easier with Guava library.

JavaScript

But how can I use the replaceValues method?

I mean this

JavaScript

Say I wanted to replace value 4.3 with a new value 5.99, how should I do that, the method expects some Iterable function and I am not sure as of how to implement it..

This is the error..

The method replaceValues(Integer, Iterable) in the type Multimap is not applicable for the arguments (int, float)

Advertisement

Answer

Multimap.replaceValues takes a collection of values that replaces all of the existing values for the given key. From the JavaDoc it looks like you need to use remove followed by put.

If the map is modifiable, you can get a modifiable view on the collection of values mapped to a single key using get, but the view returned is a plain Collection without an atomic replace method. You can always create your own helper method. Note that this method is not thread-safe.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement