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:de.hybris.platform.acceleratorstorefrontcommons.controllers.util.GlobalMessages.java

public static void addMessage(final Model model, final String messageHolder, final String messageKey,
        final Object[] attributes) {
    final GlobalMessage message = new GlobalMessage();
    message.setCode(messageKey);/*  w w w. j a  v a  2  s.  com*/
    message.setAttributes(attributes != null ? Arrays.asList(attributes) : Collections.emptyList());

    final Map<String, Object> modelMap = model.asMap();
    if (modelMap.containsKey(messageHolder)) {
        final List<GlobalMessage> messages = new ArrayList<>((List<GlobalMessage>) modelMap.get(messageHolder));
        messages.add(message);
        model.addAttribute(messageHolder, messages);
    } else {
        model.addAttribute(messageHolder, Collections.singletonList(message));
    }
}

From source file:Main.java

public static <K extends Object, T extends Object> T getFirst(Map<K, T> map, K... keys) {
    if (map == null)
        return null;
    for (K key : keys) {
        if (map.containsKey(key))
            return map.get(key);
    }/*from   w w  w  .  j  av a2s.c  o m*/
    return null;
}

From source file:com.exxonmobile.ace.hybris.storefront.controllers.util.GlobalMessages.java

public static void addMessage(final Model model, final String messageHolder, final String messageKey,
        final Object[] attributes) {
    final GlobalMessage message = new GlobalMessage();
    message.setCode(messageKey);/* w  w w .  j  a va 2 s.c  o m*/
    message.setAttributes(attributes != null ? Arrays.asList(attributes) : Collections.emptyList());

    final Map<String, Object> modelMap = model.asMap();
    if (modelMap.containsKey(messageHolder)) {
        final List<GlobalMessage> messages = new ArrayList<GlobalMessage>(
                (List<GlobalMessage>) modelMap.get(messageHolder));
        messages.add(message);
        model.addAttribute(messageHolder, messages);
    } else {
        model.addAttribute(messageHolder, Collections.singletonList(message));
    }
}

From source file:Main.java

/**
 * Returns a list of the values of the selected elements from an input map. 
 * It is not backed in the input map, thus changes in this list are not 
 * reflected in the input map.//  w  w  w  . j  ava  2  s .c  o m
 *
 * @param <A> Key type
 * @param <B> Value type
 * @param map Input map
 * @param keys Keys of the elements to be selected
 * @return List of the values associated to the input indexes (in iteration order)
 */
public static <A, B> List<B> select(Map<A, B> map, Collection<A> keys) {
    List<B> out = new LinkedList<B>();
    for (A key : keys)
        if (map.containsKey(key))
            out.add(map.get(key));

    return out;
}

From source file:Main.java

public static <K, V> void addKeyValue(Map<K, List<V>> multiMap, K key, V value) {
    if (key != null) {
        List<V> values;//from ww  w  .java 2  s.c o m
        if (multiMap.containsKey(key)) {
            values = multiMap.get(key);
        } else {
            values = new ArrayList<V>();
            multiMap.put(key, values);
        }
        values.add(value);
    }
}

From source file:Main.java

/**
 * Returns the first object in the {@link Map} whose key <b>is represented
 * by or starts with some of the characters of the specified seed</b>. This
 * method allows developers to simulate a reverse {@code startsWith}
 * operation in a {@code Map}. That is, the map key only contains some of
 * the initial characters in the seed./*from w w w  .  j  a  v  a 2 s .  c o m*/
 * <p>
 * If the map contains a complete key represented by the {@code seed}
 * parameter, such a value will be returned. Otherwise, the method will go
 * trough all the map entries looking for the first key whose characters
 * match the specified seed. If none of the keys match the seed in any way,
 * the default value will be returned. For example:
 * 
 * <pre>
 * String seed = "prefix_mapentry";
 * Map<String, String> map = new HashMap<>();
 * 
 * map.put("first.key", "1");
 * map.put("prefix_", "2");
 * map.put("third.key", "3");
 * 
 * System.out.println(getMapValue(map, seed, null));
 * </pre>
 * 
 * The following value will be printed:
 * 
 * <pre>
 * 2
 * </pre>
 * 
 * @param map
 *            - The map containing the elements to inspect.
 * @param seed
 *            - The key of the value to return.
 * @param defaultValue
 *            - The default value to return in case the key doesn't match an
 *            entry in the map.
 * @return The value mapped to the specified key, or the default value.
 */
public static <T> T getMapValue(final Map<String, T> map, final String seed, T defaultValue) {
    if (map.containsKey(seed)) {
        return map.get(seed);
    } else {
        for (Map.Entry<String, T> entry : map.entrySet()) {
            if (seed.startsWith(entry.getKey())) {
                return entry.getValue();
            }
        }
    }
    return defaultValue;
}

From source file:carmen.utils.Utils.java

@SuppressWarnings("unchecked")
public static Map<String, Object> getPlaceFromTweet(Map<String, Object> tweet) {
    if (tweet.containsKey(Constants.PLACE))
        return (Map<String, Object>) tweet.get(Constants.PLACE);
    return null;//from   w  w w  .  j  a v a 2  s . co m
}

From source file:carmen.utils.Utils.java

@SuppressWarnings("unchecked")
public static Map<String, Object> getUserFromTweet(Map<String, Object> tweet) {
    if (tweet.containsKey(Constants.TWEET_USER))
        return (Map<String, Object>) tweet.get(Constants.TWEET_USER);
    return null;/*from ww  w.j a v  a2  s .  co  m*/
}

From source file:Main.java

/**
 *  Retrieves the first value of a MultiMap index
 *  @param multiMap (Map<String,LinkedList<String>>)
 *  @param index (String)/*from   ww  w.  ja  v a  2 s. c om*/
 *  @return String
 **/
public static String popMultiMap(Map<String, LinkedList<String>> multiMap, String key) {
    if (multiMap == null)
        return (null);

    if (multiMap.containsKey(key)) {
        LinkedList<String> list = multiMap.get(key);
        if (list != null) {
            return (list.pop());
        } else {
            return (null);
        }
    } else {
        return (null);
    }
}

From source file:Main.java

/**
 * Removes any keys not found in the element collection provided in this method
 * @param map/*w w w  .ja  v a  2  s  .  c  om*/
 * @param keysToKeep
 * @return
 */
public static <K, V> Map<K, V> filterKeys(Map<K, V> map, K... keysToKeep) {
    Map<K, V> filteredMap = new HashMap<K, V>();
    for (K key : keysToKeep) {
        if (map.containsKey(key)) {
            filteredMap.put(key, map.get(key));
        }
    }
    return filteredMap;
}