java 在另一个ImmutableMap上叠加/合并ImmutableMap

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 在另一个ImmutableMap上叠加/合并ImmutableMap相关的知识,希望对你有一定的参考价值。

import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

public class MapTransforms
{
    /**
     * This assumes that <code>defaults</code> specifies the complete domain of the set of keys/values supported by <code>overlay</code>.
     * <p/>
     * This is implemented as a <code>View</code> as described in the documentation for {@link com.google.common.collect.Maps#transformEntries(Map, Maps.EntryTransformer)} and does not copy either map and returns the values on demand.
     * This means if you want to transform the entire <code>Map</code> to the merged version at once you need to wrap the return value in <code>ImmutableMap.copyOf()</code> which will eagerly
     * apply the transformation and process the entire map only once.
     * <p/>
     * There is no sanity check on this that a given key exists in the <code>defaults.keySet()</code>
     *
     * @see com.google.common.collect.Maps.EntryTransformer
     * @param defaults complete domain to overlay new <code>Map</code> onto
     * @param overlay  <code>Map</code> contents to replace what is contained in <code>defaults</code>
     * @param <K>      Key type
     * @param <V>      Value type
     * @return returns a <code>View</code> as an <code>ImmutableMap</code> that returns the values in <code>overlay</code> if they exist otherwise the value in <code>defaults</code>.
     */
    public static <K, V> Map<K, V> overlay(@Nonnull final Map<K, V> defaults, @Nonnull final Map<K, V> overlay)
    {
        return Maps.transformEntries(defaults, new Maps.EntryTransformer<K, V, V>()
        {
            @Override
            public V transformEntry(@Nullable final K key, @Nullable final V value)
            {
                return overlay.containsKey(key) ? overlay.get(key) : value;
            }
        });
    }

    /**
     * This does the same thing that {@link #overlay(Map, Map)} does, but wraps the return value in {@link ImmutableMap#copyOf(Map)} to eagerly apply the transform.
     *
     * @param defaults complete domain to overlay new <code>Map</code> onto
     * @param overlay  <code>Map</code> contents to replace what is contained in <code>defaults</code>
     * @param <K>      Key type
     * @param <V>      Value type
     * @return applyes {@link #overlay(Map, Map)} and wraps the result in {@link ImmutableMap#copyOf(Map)}
     */
    public static <K,V> Map<K,V> merge(@Nonnull final Map<K,V> defaults, @Nonnull final Map<K,V> overlay)
    {
        return ImmutableMap.copyOf(overlay(defaults,overlay));
    }
}

以上是关于java 在另一个ImmutableMap上叠加/合并ImmutableMap的主要内容,如果未能解决你的问题,请参考以下文章

java ImmutableMap使用

java ImmutableMap使用

ffmpeg在另一个上叠加视频

在Android中使用FFMPEG在另一个视频上循环叠加视频

网络叠加模式VLANVxLANGRE

如何在离子中将图像叠加在另一个图像之上