Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a mutable collection from an immutable one

Tags:

java

guava

Let's say I have the following Map which is created using Guava's library: (List<Integer> is also immutable)

Map<String, List<Integer>> map = ImmutableMap.builder()...

I pass this map to a class where I want to create a mutable copy of it and modify it. It is of course possible to do it manually, but is there a way to convert a nested immutable collection back to a mutable one?

like image 273
Wickoo Avatar asked Sep 30 '22 03:09

Wickoo


1 Answers

As pointed out, I'd use an ImmutableListMultimap<String, Integer> instead of a ImmutableMap<String, ImmutableList<Integer>>.

Then if you want a mutable copy, you can just pass the immutable multimap to the create static factory method on one of the mutable ListMultimap implementations (ArrayListMultimap or LinkedListMultimap).

like image 112
Kurt Alfred Kluever Avatar answered Oct 05 '22 06:10

Kurt Alfred Kluever