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

/**
 * Utility function to determine the presence of a key. 
 * The default is false.//w w w.  j  a v a 2 s .  c  o  m
 * 
 * @param key the string for the key
 * @param map the Map to check for the presence 
 * and value of the key
 * @return true if the value for the key is true, otherwise false
 */
public static boolean isValue(final String key, final Map map) {
    if (map == null) {
        return false;
    }
    Object o = map.get(key);
    if (!(o instanceof Boolean)) {
        return false;
    }
    return ((Boolean) o).booleanValue();
}

From source file:Main.java

/**
 * Decrements the integer value of the given key by the given increment. In
 * case the given key does not exists, the key is associated with the given
 * default value./* w w  w .  j av  a 2s .c  o m*/
 *
 * @param <T> The type of the keys of the given map.
 * @param map The given map to decrement the given key of.
 * @param key The given key.
 * @param decrement The value to decrement the value associated to the given
 * key with.
 * @param deflt The value to associate the given key with, in case the key
 * is not yet added to the map.
 * @return The new value associated with the given key.
 */
public static <T> int decrementKey(Map<T, Integer> map, T key, int decrement, int deflt) {
    Integer val = map.get(key);
    if (val == null) {
        val = deflt;
    } else {
        val -= decrement;
    }
    map.put(key, val);
    return val;
}

From source file:at.tuwien.ifs.somtoolbox.util.CollectionUtils.java

public static HashSet<String> getOrCreateValue(Map<String, HashSet<String>> map, String key) {
    if (map.get(key) == null) {
        map.put(key, new HashSet<String>());
    }/*from   ww w . jav a 2 s.  c  o m*/
    return map.get(key);
}

From source file:Main.java

/**
 * Puts passed value to passed {@link Map} instance on passed key of such
 * does not contained or its associated key does not equals passed value
 * //w w  w.ja va 2s  .c o m
 * @param map
 * @param key
 * @param value
 */
public static <K, V> void checkAndAdd(Map<K, V> map, K key, V value) {

    boolean contained = map.containsKey(key) && value.equals(map.get(key));
    if (Boolean.FALSE.equals(contained)) {
        map.put(key, value);
    }
}

From source file:jj.resource.FileTypeSettingsDefaultProvider.java

private static ResourceSettings makeSettings(Map<String, Object> input) {
    String mimeType = (String) input.get("mimeType");
    Charset charset = input.containsKey("charset") ? Charset.forName((String) input.get("charset")) : null;
    boolean compressible = input.containsKey("compressible") && Boolean.TRUE.equals(input.get("compressible"));

    return new ResourceSettings(mimeType, charset, compressible);
}

From source file:Main.java

private static final int getFreq(final Object obj, final Map freqMap) {
    Integer count = (Integer) freqMap.get(obj);
    if (count != null) {
        return count.intValue();
    }//  w ww .  ja va 2 s. c o  m
    return 0;
}

From source file:Main.java

public static <K, V> void addToMap(K key, V value, Map<K, Set<V>> map) {

    if (key != null && value != null)
        if (map.containsKey(key))
            map.get(key).add(value);
        else {//from  w  ww.j a v  a  2  s . co m
            Set<V> values = new TreeSet<V>();
            values.add(value);
            map.put(key, values);
        }
}

From source file:Main.java

/**
 * Sets the value of the entry in the map for the given key, though if the
 * map already contains a value for the given key then the value is appended
 * to a list of values./*w  w w  .j a va  2 s  .  c  o m*/
 *
 * @param map the map to add the entry to
 * @param key the key in the map
 * @param value the value to put in the map
 */
@SuppressWarnings("unchecked")
public static void appendValue(Map<String, Object> map, String key, Object value) {
    Object oldValue = map.get(key);
    if (oldValue != null) {
        List<Object> list;
        if (oldValue instanceof List) {
            list = (List<Object>) oldValue;
        } else {
            list = new ArrayList<Object>();
            list.add(oldValue);
            // replace old entry with list
            map.remove(key);
            map.put(key, list);
        }
        list.add(value);
    } else {
        map.put(key, value);
    }
}

From source file:Main.java

public static <T> T mostCommon(List<T> list, double percent) {
    if (list.isEmpty())
        return null;

    Map<T, Integer> map = new HashMap<>();

    for (T t : list) {
        Integer count = map.get(t);
        map.put(t, count == null ? 1 : count + 1);
    }/*from w  ww  .  j av  a  2s  .co m*/

    double freq = map.size() / (double) list.size();
    if (1.0 == freq || (1.0 - freq) < percent)
        return null;

    Entry<T, Integer> max = null;
    for (Entry<T, Integer> e : map.entrySet()) {
        if (max == null || e.getValue() > max.getValue()) {
            max = e;
        }
    }

    return max.getKey();
}

From source file:Main.java

/**
 * @param map to collect//w ww  . ja  va  2s. c om
 * @param keys to collect from map
 * @param to add each values for given keys from given map
 * @param <K> key type
 * @param <T> value type
 * @param <C> collectin type to collect result to
 * @return the provided to collection with given keys from map appended
 */
public static <K, T, C extends Collection<? super T>> C collect(Map<K, T> map, Collection<K> keys, C to) {
    for (K key : keys) {
        to.add(map.get(key));
    }
    return to;
}