Example usage for java.util Map get

List of usage examples for java.util Map get

Introduction

In this page you can find the example usage for java.util Map get.

Prototype

V get(Object key);

Source Link

Document

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Usage

From source file:Main.java

/**
 * Returns set from a map referenced by the given key. If the set is not
 * present under the given key, a new list is created, inserted into the map
 * and returned. In other words, it ensures retrieval of a set for the given
 * key.// w w  w .  j a va2s.  c o  m
 *
 * @param <Key>   key type of the map
 * @param <Value> value type of the set (which acts as a value in the map)
 * @param map     key-value map, where value is of type {@link Set}
 * @param node    key
 * @return non-null {@link Set} for the given key
 */
public static <Key, Value> Set<Value> getSet(Map<Key, Set<Value>> map, Key node) {
    Set<Value> set = map.get(node);
    if (set == null) {
        set = new HashSet<>();
        map.put(node, set);
    }
    return set;
}

From source file:Main.java

public static <K1, K2, V> Map<K2, V> mapGetOrCreateMap(Map<K1, Map<K2, V>> mapOfMaps, K1 firstKey) {
    Map<K2, V> m = mapOfMaps.get(firstKey);
    if (null == m) {
        m = newMap();/*from w  w  w  .  j a  v a2  s.c o  m*/
        mapOfMaps.put(firstKey, m);
    }
    return m;
}

From source file:Main.java

public static String getDeviceProperty(Map<String, String> deviceProperties, String property) {
    return deviceProperties.get(property);
}

From source file:Main.java

/**
 * Returns list from a map referenced by the given key. If the list is not
 * present under the given key, a new list is created, inserted into the map
 * and returned. In other words, it ensures retrieval of a list for the
 * given key./*from   w  w w.  j  a v a 2s . co  m*/
 *
 * @param <Key>   key type of the map
 * @param <Value> value type of the list (which acts as a value in the map)
 * @param map     key-value map, where value is of type {@link List}
 * @param node    key
 * @return non-null {@link List} for the given key
 */
public static <Key, Value> List<Value> getList(Map<Key, List<Value>> map, Key node) {
    List<Value> list = map.get(node);
    if (list == null) {
        list = new ArrayList<>();
        map.put(node, list);
    }
    return list;
}

From source file:Main.java

public static <K, V> SortedSet<V> getEntryOrEmptySet(K key, Map<K, SortedSet<V>> map) {
    return (map.containsKey(key) ? map.get(key) : new TreeSet<V>());
}

From source file:Main.java

/**
 * Add {@code value} to collection {@code map}.get({@code key}). If {@code key} does not exists
 * yet, set it to {@code emptyCollection} before addition.
 *///w  ww.j av a  2 s  .co m
public static <K, V extends Collection<E>, E> void upsert(Map<K, V> map, K key, E value, V emptyCollection) {
    V collection = map.get(key);
    if (collection == null) {
        collection = emptyCollection;
    }
    collection.add(value);
    map.put(key, collection);
}

From source file:Main.java

/**
 * Return the mapped value of the specified key k.
 * If no such key k, return the specified value v.
 * @param <K>/* ww w.j  a v a  2 s.  c o  m*/
 * @param <V>
 * @param map
 * @param k
 * @param v the default value if no key exist.
 * @return
 */
public static <K, V> V get(Map<K, V> map, K k, V v) {
    if (map.containsKey(k))
        return map.get(k);
    return v;
}

From source file:Main.java

public static Object getKeyFromValue(Map map, Object value) {
    for (Object o : map.keySet()) {
        if (map.get(o).equals(value)) {
            return o;
        }//from   w  w  w  .  j  a  v a2  s  .  c o m
    }

    return null;
}

From source file:Main.java

public static <K, V> void appendToMultiMap(final Map<K, List<V>> mm, final K key, final V value) {
    List<V> values = mm.get(key);
    if (values == null) {
        values = new ArrayList<V>();
        mm.put(key, values);//w  w  w.  j  ava2 s  .c o  m
    }
    values.add(value);
}

From source file:Main.java

/**
 * Adds the value to map. If the key does not exists new value set is created and the value is
 * added to it//from   w w w  .ja  v  a  2 s.  c  o m
 * 
 * @param <K>
 *            the key type
 * @param <V>
 *            the value type
 * @param map
 *            the map
 * @param key
 *            the key
 * @param newValue
 *            the new value
 */
public static <K, V> void addValueToSetMap(Map<K, Set<V>> map, K key, V newValue) {
    Set<V> list = map.get(key);
    if (list == null) {
        list = new LinkedHashSet<V>();
        map.put(key, list);
    }
    list.add(newValue);
}