簡體   English   中英

從不可變的集合創建可變集合

[英]Creating a mutable collection from an immutable one

假設我有使用Guava庫創建的以下Map :( List<Integer>也是不可變的)

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

我將此映射傳遞給一個類,我想在其中創建一個可變副本並對其進行修改。 它當然可以手動完成,但有沒有辦法將嵌套的不可變集合轉換回可變集合?

正如所指出的,我使用ImmutableListMultimap<String, Integer>而不是ImmutableMap<String, ImmutableList<Integer>>

然后,如果您想要一個可變副本,您可以將不可變ListMultimap映射傳遞給其中一個可變ListMultimap實現( ArrayListMultimapLinkedListMultimap )上的create static factory方法。

這是我的解決方案。 設置它需要相當多的代碼,但一旦完成它就非常容易使用。

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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM