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

/**
 * @param <T>/*from w  w w . jav  a2 s  . c  om*/
 * @param aMap
 * @param aKey
 * @param aCount
 * @return New Count Value
 */
public static <T> int incrementCount(Map<T, Integer> aMap, T aKey, Integer aCount) {
    Integer count = aMap.get(aKey);
    if (count == null) {
        count = 0;
    }

    int newCount = count + aCount;
    aMap.put(aKey, newCount);
    return newCount;
}

From source file:Main.java

/** Find all values of a named field in a nested Map containing fields, Maps, and Collections of Maps (Lists, etc) */
public static void findAllFieldsNestedMap(String key, Map theMap, Set<Object> valueSet) {
    Object localValue = theMap.get(key);
    if (localValue != null)
        valueSet.add(localValue);/*from   w  ww .  j  a va 2  s.c  o  m*/
    for (Object value : theMap.values()) {
        if (value instanceof Map) {
            findAllFieldsNestedMap(key, (Map) value, valueSet);
        } else if (value instanceof Collection) {
            // only look in Collections of Maps
            for (Object colValue : (Collection) value) {
                if (colValue instanceof Map)
                    findAllFieldsNestedMap(key, (Map) colValue, valueSet);
            }
        }
    }
}

From source file:Main.java

@Deprecated
public static void filterList(List<Map<String, Object>> list, String key) {

    List<Object> keys = new ArrayList<>();
    for (Map<String, Object> map : list) {

        Object object = map.get(key);

        if (!keys.contains(object)) {

            keys.add(object);// ww w. java  2s  .c o m
        } else {

            list.remove(map);
        }
    }
}

From source file:Main.java

/**
 * Copy value.//from  w  w w .j a va  2s .  c  om
 *
 * @param <K>
 *            the key type
 * @param <V>
 *            the value type
 * @param source
 *            the source
 * @param target
 *            the target
 * @param key
 *            the key
 */
public static <K, V> void copyValue(Map<K, V> source, Map<K, V> target, K key) {
    target.put(key, source.get(key));
}

From source file:Main.java

private static void incrementCardCount(Map<String, Integer> map, String blueprintId, int incrementBy) {
    final Integer oldCount = map.get(blueprintId);
    if (oldCount == null)
        map.put(blueprintId, incrementBy);
    else//from ww w.  j a  v  a  2  s  .  c  om
        map.put(blueprintId, oldCount + incrementBy);
}

From source file:Main.java

public static Map<String, Object> getFieldDefinition(Map<String, Object> flow, String fieldName) {
    return (Map<String, Object>) ((Map<String, Object>) flow.get("fields")).get(fieldName);
}

From source file:Main.java

public static String getQueryValue(String url, String name) {
    try {//  www .j  a  v a  2s .  c om
        URL dUrl = new URL("http://" + url);
        Map<String, String> query = getQueryMap(dUrl.getQuery());
        return query.get(name);

    } catch (Exception e) {
    }

    return null;
}

From source file:Main.java

/**
 * Copy value from the source map to the target map only if the value exists in the source map.
 * //ww  w.ja v  a  2  s. co  m
 * @param <K>
 *            the key type
 * @param <V>
 *            the value type
 * @param source
 *            the source map
 * @param target
 *            the target map
 * @param key
 *            the key to copy
 * @return <code>true</code> if exists and copied
 */
public static <K, V> boolean copyValueIfExist(Map<K, V> source, Map<K, V> target, K key) {
    V v = source.get(key);
    if (v != null) {
        target.put(key, v);
        return true;
    }
    return false;
}

From source file:Main.java

/**
 * Add value to list of values in the map. Creates a new list if it doesn't exist for the key
 *
 * @param map/*from   www. ja  va 2 s  .c o  m*/
 * @param key
 * @param value
 * @return the updated list of values.
 */
public static <K, V> List<V> addToValueList(Map<K, List<V>> map, K key, V value) {

    List<V> values = map.get(key);
    if (values == null) {
        values = new ArrayList<V>();
        map.put(key, values);
    }
    values.add(value);

    return values;
}

From source file:Main.java

/**
 * Returns the map value for the given key, creating and associating a new,
 * empty {@link HashMap} if the key's value is null.
 * //from  w ww.  jav  a2  s  .  co  m
 * @param <I>
 *            The index key type.
 * @param <K>
 *            The map's key type.
 * @param <V>
 *            The map's value type.
 * @param mapMap
 *            The map to evaluate.
 * @param idxKey
 *            The index key to fetch.
 * @return A collection of values for the given key.
 */
public static <I, K, V> Map<K, V> initMapValue(final Map<I, Map<K, V>> mapMap, final I idxKey) {
    Map<K, V> indexedMap = mapMap.get(idxKey);
    if (indexedMap == null) {
        indexedMap = new HashMap<K, V>();
        mapMap.put(idxKey, indexedMap);
    }
    return indexedMap;
}