Example usage for java.util Collections unmodifiableMap

List of usage examples for java.util Collections unmodifiableMap

Introduction

In this page you can find the example usage for java.util Collections unmodifiableMap.

Prototype

public static <K, V> Map<K, V> unmodifiableMap(Map<? extends K, ? extends V> m) 

Source Link

Document

Returns an unmodifiable view of the specified map.

Usage

From source file:Main.java

public static <K, V> Map<K, V> copyNullSafeHashMapWithNullValues(Map<? extends K, ? extends V> map) {
    if (map.isEmpty()) {
        return Collections.emptyMap();
    }/*from   ww w .j  a  v  a 2s.c om*/
    return Collections.unmodifiableMap(copyNullSafeMutableHashMapWithNullValues(map));
}

From source file:Main.java

public static <K, V> Map<K, V> immutableMap(Collection<Map.Entry<K, V>> entrySet) {
    Map<K, V> resultMap = new HashMap<K, V>();
    for (Map.Entry<K, V> entry : entrySet) {
        if (entry != null)
            resultMap.put(entry.getKey(), entry.getValue());
    }/*from  w  w  w  .  j  a v  a  2s  .  com*/
    return Collections.unmodifiableMap(resultMap);
}

From source file:Main.java

public static <K, V> Map<K, V> immutableMap(Map<K, V> map) {
    return Collections.unmodifiableMap(new LinkedHashMap<>(map));
}

From source file:Main.java

public static <K extends Object, V extends Object> Map<K, V> unmodifiableMap(
        final Map<? extends K, ? extends V> map) {
    return Collections.unmodifiableMap(map);
}

From source file:Main.java

/**
 * @param entries the <i>final</i> set of entries to add to the newly created <i>unmodifiable</i> map
 * @return an <i>unmodifiable</i> map with all given entries
 *//*from w w w  .j ava2  s.  com*/
public static <K, V> Map<K, V> map(final Entry<K, V>... entries) {
    final HashMap<K, V> map = new HashMap<K, V>(entries.length);
    for (final Entry<K, V> entry : entries) {
        map.put(entry.getKey(), entry.getValue());
    }
    return Collections.unmodifiableMap(map);
}

From source file:Main.java

static <K, V> Map<K, V> getMap(Map<K, V> orig) {
    if (orig == null || orig.size() == 0) {
        return Collections.emptyMap();
    } else if (orig.size() == 1) {
        final Map.Entry<K, V> entry = orig.entrySet().iterator().next();
        return Collections.singletonMap(entry.getKey(), entry.getValue());
    } else {/*from  w  w  w  . j av a2 s  .  c  om*/
        return Collections.unmodifiableMap(new TreeMap<>(orig));
    }
}

From source file:io.github.carlomicieli.footballdb.starter.utils.MapUtils.java

/**
 * Creates a new immutable map from the pair available in the provided stream.
 * @param pairs//from  w  ww .j  av a  2  s  .  c o  m
 * @param <K>
 * @param <V>
 * @return
 */
public static <K, V> Map<K, V> newMap(Stream<ImmutablePair<K, V>> pairs) {
    return Collections
            .unmodifiableMap(pairs.collect(Collectors.toMap(ImmutablePair::getLeft, ImmutablePair::getRight)));
}

From source file:Main.java

/**
 * Returns an unmodifiable view of the specified {@code mapSet}. This
 *  method allows modules to provide users with "read-only" access to
 * {@link Map}, but also to the value {@link Set}.
 *
 * @param <K>/*ww w  . j  ava 2s  . co m*/
 *            the class of the map keys
 * @param <V>
 *            the class of the set values
 * @param mapSet
 *            the {@link Map} of {@link Set} for which an unmodifiable view
 *            is to be returned
 * @return an unmodifiable view of the specified map.
 *
 * @see Collections#unmodifiableMap(Map)
 * @see Collections#unmodifiableSet(Set)
 * @since 4.2
 */
@Nonnull
public static <K, V> Map<K, Set<V>> unmodifiableMapSet(@Nullable final Map<K, Set<V>> mapSet) {
    if (mapSet == null) {
        return Collections.emptyMap();
    }

    for (final Map.Entry<K, Set<V>> entry : mapSet.entrySet()) {
        final K key = entry.getKey();
        final Set<V> value = entry.getValue();

        mapSet.replace(key, Collections.unmodifiableSet(value));
    }

    return Collections.unmodifiableMap(mapSet);
}

From source file:Main.java

/**
 * Make a list map immutable.//from  w  w  w  . j  a  v  a  2  s.  c o m
 *
 * @param listMap
 *            the list map in input.
 * @return the immutable list map.
 */
public static <C, E> Map<C, List<E>> makeListMapImmutable(Map<C, List<E>> listMap) {
    for (C key : listMap.keySet()) {
        listMap.put(key, Collections.unmodifiableList(listMap.get(key)));
    }

    return Collections.unmodifiableMap(listMap);
}

From source file:Main.java

/**
 * Functions as per {@link Collections#unmodifiableMap(Map)} with the exception that if the
 * given map is null, this method will return an unmodifiable empty map as per
 * {@link Collections#emptyMap()}.//from  w  ww .  j a va2s  . c om
 * 
 * @param map the map for which an unmodifiable view is to be returned
 * @return an unmodifiable view of the specified set, or an unmodifiable empty set if the given
 *         set is null
 */
public static <K, V> Map<K, V> unmodifiableMapNullSafe(Map<? extends K, ? extends V> map) {
    if (map == null) {
        return Collections.emptyMap();
    }
    return Collections.unmodifiableMap(map);
}