Example usage for java.util Map containsKey

List of usage examples for java.util Map containsKey

Introduction

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

Prototype

boolean containsKey(Object key);

Source Link

Document

Returns true if this map contains a mapping for the specified key.

Usage

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
 * //from   w w  w . j ava 2  s.co 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: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);//w w w .j a  va2  s.c  om
        else {
            Set<V> values = new TreeSet<V>();
            values.add(value);
            map.put(key, values);
        }
}

From source file:Main.java

public static <T> Set removeRepetition(Collection<T> ts) {
    if (ts == null || ts.isEmpty()) {
        return Collections.emptySet();
    }/*from   ww  w .j  a va 2s .  c o m*/
    Map<T, Object> map = new LinkedHashMap();
    for (T t : ts) {
        if (!map.containsKey(t)) {
            map.put(t, -1);
        }
    }
    Set<T> set = map.keySet();
    return set;
}

From source file:Main.java

public static <T> Map<T, Integer> frequency(Collection<? extends T> collection) {
    Map<T, Integer> res = new TreeMap<>();
    for (T element : collection) {
        if (!res.containsKey(element)) {
            res.put(element, Collections.frequency(collection, element));
        }/*from   w  w  w  . ja v  a 2 s .c o m*/
    }
    return res;
}

From source file:Main.java

public static <K, V> Map<K, Set<V>> groupByKeyEmpty(List<K> keys) {
    Map<K, Set<V>> map = new HashMap<K, Set<V>>();
    for (K key : keys) {
        if (!map.containsKey(key)) {
            map.put(key, new HashSet<V>());
        }//from w ww.ja  v  a  2 s.c  om
    }
    return map;
}

From source file:com.flaptor.indextank.query.analyzers.StopAnalyzer.java

private static Set<String> getStopWords(Map<Object, Object> analyzerConfiguration) {
    if (analyzerConfiguration.containsKey(STOPWORDS)) {
        JSONArray stopwordList = (JSONArray) analyzerConfiguration.get(STOPWORDS);
        Set<String> stopwords = new HashSet<String>(stopwordList.size());
        for (Object stopword : stopwordList) {
            if (!(stopword instanceof String)) {
                throw new IllegalArgumentException("Stopwords aren't Strings");
            }/*  w  ww  .  j  a va2  s . c  om*/
            stopwords.add((String) stopword);
        }
        return stopwords;
    } else {
        return ImmutableSet.of();
    }
}

From source file:Main.java

public static <T, U> boolean containsKey(Map<T, U> map, T key) {
    if (map == null) {
        return false;
    }/*from  w  ww.j  a  v  a2s .  com*/
    return map.containsKey(key);
}

From source file:org.eclipse.swordfish.core.configuration.xml.SaxParsingPrototype.java

private static void putElement(Map<String, List<String>> props, String key, String value) {
    if (props.containsKey(key)) {
        props.get(key).add(value);/*from   ww w  . j  a v  a2s .  c o m*/
    } else {
        List<String> list = new LinkedList<String>();
        list.add(value);
        props.put(key, list);
    }
}

From source file:Main.java

public static String getMapValue(Map<String, Object> map, String key) {
    String value = "";
    if (map != null) {
        if (map.containsKey(key) && map.get(key) != null) {
            value = (String) map.get(key);
        }/*from  www .  ja v a  2s  . com*/
    }
    return value;
}

From source file:Main.java

public static <K> Map safeGetMap(Map<K, Object> map, K key) {
    if (map == null) {
        return null;
    } else {/*from  w w  w . ja  v a 2s  .co m*/
        return map.containsKey(key) ? (Map) map.get(key) : null;
    }
}