Is there a way to create (/return) an unmodifiable Multimap?
I am using the Multimap of com.google.common.collect and need to create an unmodfiable Multimap, but I couldn't find a lib that has a method, which returns the a given Multimap as an umodifiable Multimap.
For "normal" Maps I usually use the MapUtils.unmodifiableMap(map) method of the org.apache.commons.collections4.MapUtils lib.
example:
public static Map<String, String> bufferName()
{
Map<String, String> fooMap = new HashMap<>();
fooMap.put("foo", "bar");
return MapUtils.unmodifiableMap(fooMap);
}
what I need:
public static Multimap<String, String> bufferName1()
{
HashMultimap<String, String> fooMultimap = HashMultimap.create();
fooMultimap.put("foo", "bar");
fooMultimap.put("foo", "foobar");
// ! the next line needs to be optimized so that the returned HashMultimap cannot be modified.
return fooMultimap;
}
I tried to find a lib, that can return given mutable Multimaps as immutable Multimaps, but couldn't find any.
I also tried to put the mutable Multimap into an immmutable Object, but that is everything else than clean.
example:
public static Set<HashMultimap<String, String>> bufferName1()
{
HashMultimap<String, String> fooMultimap = HashMultimap.create();
fooMultimap.put("foo", "bar");
fooMultimap.put("foo", "foobar");
// org.apache.commons.collections4.SetUtils
SetUtils.unmodifiableSet(Set.of(fooMultimap));
}