Creating a mutable collection from immutable

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

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

I pass this map to the class where I want to create a mutable copy and modify it. Of course, this can be done manually, but is there a way to convert a nested immutable collection back to mutable?

+5
source share
2 answers

As indicated, I would use ImmutableListMultimap<String, Integer> instead of ImmutableMap<String, ImmutableList<Integer>> .

Then, if you need a modified copy, you can simply pass the immutable multimag to the create static factory method in one of the mutable ListMultimap implementations ( ArrayListMultimap or LinkedListMultimap ).

+2
source

Here is my solution. There is enough code to configure it, but after that it is very easy to use.

 public class Main { // UnaryOperator and identity are in Java 8. // I include them here in case you are using an earlier version. static interface UnaryOperator<T> { T apply(T t); } static <T> UnaryOperator<T> identity() { return new UnaryOperator<T>() { @Override public T apply(T t) { return t; } }; } // This unary operator turns any List into an ArrayList. static <E> UnaryOperator<List<E>> arrayList(final UnaryOperator<E> op) { return new UnaryOperator<List<E>>() { @Override public List<E> apply(List<E> list) { List<E> temp = new ArrayList<E>(); for (E e : list) temp.add(op.apply(e)); return temp; } }; } // This unary operator turns any Set into a HashSet. static <E> UnaryOperator<Set<E>> hashSet(final UnaryOperator<E> op) { return new UnaryOperator<Set<E>>() { @Override public Set<E> apply(Set<E> set) { Set<E> temp = new HashSet<E>(); for (E e : set) temp.add(op.apply(e)); return temp; } }; } // This unary operator turns any Map into a HashMap. static <K, V> UnaryOperator<Map<K, V>> hashMap(final UnaryOperator<K> op1, final UnaryOperator<V> op2) { return new UnaryOperator<Map<K, V>>() { @Override public Map<K, V> apply(Map<K, V> map) { Map<K, V> temp = new HashMap<K, V>(); for (Map.Entry<K, V> entry : map.entrySet()) temp.put(op1.apply(entry.getKey()), op2.apply(entry.getValue())); return temp; } }; } public static void main(String[] args) { // In this example I will first create an unmodifiable collection of unmodifiable collections. Map<String, List<Set<Integer>>> map = new HashMap<String, List<Set<Integer>>>(); map.put("Example", Collections.unmodifiableList(Arrays.asList(Collections.unmodifiableSet(new HashSet<Integer>(Arrays.asList(1, 2, 3)))))); map = Collections.unmodifiableMap(map); // Now I will make it mutable in one line! map = hashMap(Main.<String>identity(), arrayList(hashSet(Main.<Integer>identity()))).apply(map); } } 
+1
source

Source: https://habr.com/ru/post/1209897/


All Articles