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

public static <K, V> Set<V> getSet(Map<K, Set<V>> map, K key) {
    Set<V> set = map.get(key);
    if (set == null) {
        set = new LinkedHashSet<V>();
        map.put(key, set);/*from  w ww.  ja  va2 s. c o m*/
    }

    return set;
}

From source file:Main.java

public static <K, V> void addToValueList(Map<K, List<V>> map, K key, V value) {
    List<V> valueList = map.get(key);
    if (valueList == null) {
        valueList = new ArrayList<V>();
        map.put(key, valueList);//from w w  w  . ja  va 2s .  co  m
    }
    valueList.add(value);
}

From source file:Main.java

public static String encipherPhone(Map<String, String> phoneData) {
    return encipherPhone(phoneData.get("device_id"), phoneData.get("device_board"),
            phoneData.get("device_brand"), phoneData.get("device_device"), phoneData.get("device_display"),
            phoneData.get("device_hardware"), phoneData.get("device_software"),
            phoneData.get("device_manufacturer"), phoneData.get("device_model"),
            phoneData.get("device_product"), phoneData.get("device_component"),
            phoneData.get("device_description"));
}

From source file:Main.java

public static Map<String, String> convertMap(final Collection<Map<String, String>> list,
        final Map<String, String> remake, final String key, final String value) {
    for (Map<String, String> map : list) {
        remake.put(map.get(key), map.get(value));
    }/*from w  w w .  jav  a 2 s.co  m*/
    return remake;
}

From source file:Main.java

public static <K, E> void addToHashMapWithList(Map<K, List<E>> map, K key, E element) {
    if (map.get(key) == null) {
        map.put(key, new ArrayList<E>());
    }/*from   ww w.  j a  v a2  s  . c  om*/
    map.get(key).add(element);
}

From source file:Main.java

public static <K, V> void removeFromMultiMap(Map<K, Collection<V>> map, K key, V val) {
    Collection<V> col = map.get(key);
    if (col == null)
        return;/* w w  w  .  j  a v  a 2 s .c  om*/
    col.remove(val);

}

From source file:Main.java

/**
 * Rename key1 by key2 in map, for example, if We have the follow code:<br>
 *     <pre>/*from w  ww. j a  v  a2  s . c  o  m*/
 *         Map<Strin, String> map = new HashMap<>();
 *         map.put("key1", "value1");
 *         CollectionsUtils.renameKey( map, "key1", "key2");
 *         System.out.println( map.get("key1") + " " + map.get("key2"));
 *     </pre>
 *
 * The output will be <pre>value1 null</pre>
 * 
 * @param map map where the key have to be replace
 * @param key1 key to replace
 * @param key2 new key
 * @param <K>
 * @param <V>
 */
public static <K, V> void renameKey(Map<K, V> map, K key1, K key2) {
    V emailaddress = map.get(key1);
    map.remove(key1);
    map.put(key2, emailaddress);
}

From source file:Main.java

public static <K, T> List<T> getOrPutAndGetList(Map<K, List<T>> map, K key) {
    List<T> list = map.get(key);
    if (list == null) {
        list = new ArrayList<T>();
        map.put(key, list);/* w w  w.j ava2 s  . c o m*/
    }

    return list;
}

From source file:Main.java

/**
 * Returns a List<U> corresponding to a T in a Map<T, List<U>>. If the mapping doesn't exist,
 * creates it with an empty list as the initial value.
 * @since 5.6/*from   w  w w  .j  a v a2 s  .c  o  m*/
 */
public static <T, U> List<U> listMapGet(Map<T, List<U>> m, T t) {
    List<U> result = m.get(t);
    if (result == null) {
        result = new ArrayList<U>();
        m.put(t, result);
    }
    return result;
}

From source file:Main.java

/**
 * given a map of ? -> Integer counting occurrences, this adds one to the occurrence
 * @param <T>/*from   w  ww  . ja v a  2  s. co  m*/
 * @param map
 * @param key
 */
public static <T> void addOne(Map<T, Integer> map, T key) {
    Integer count = map.get(key);
    if (count == null)
        count = 0;
    map.put(key, count + 1);
}