Example usage for java.util Map entrySet

List of usage examples for java.util Map entrySet

Introduction

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

Prototype

Set<Map.Entry<K, V>> entrySet();

Source Link

Document

Returns a Set view of the mappings contained in this map.

Usage

From source file:io.adeptj.runtime.server.IdmUtil.java

static Account verifyCredentials(Map<String, List<String>> userRolesMapping, String id,
        PasswordCredential credential) {
    return userRolesMapping.entrySet().stream()
            .filter(entry -> IdmUtil.verifyCredentials(entry.getKey(), id, credential.getPassword()))
            .map(entry -> new SimpleAccount(new SimplePrincipal(entry.getKey()),
                    new HashSet<>(entry.getValue())))
            .findFirst().orElse(null);// ww  w . j a va 2  s . c  o m
}

From source file:Main.java

public static void addBigDecimalsInMap(Map<String, Object> baseMap, Map<String, Object> addMap) {
    if (baseMap == null || addMap == null)
        return;/*www.  ja  v  a  2 s .com*/
    for (Map.Entry<String, Object> entry : addMap.entrySet()) {
        if (!(entry.getValue() instanceof BigDecimal))
            continue;
        BigDecimal addVal = (BigDecimal) entry.getValue();
        Object baseObj = baseMap.get(entry.getKey());
        if (baseObj == null || !(baseObj instanceof BigDecimal))
            baseObj = BigDecimal.ZERO;
        BigDecimal baseVal = (BigDecimal) baseObj;
        baseMap.put(entry.getKey(), baseVal.add(addVal));
    }
}

From source file:Main.java

public static <K, V> Map<V, K> reverseKeyValue(Map<K, V> map) {
    if (map == null) {
        return null;
    }// w  w w.  j  av a2  s .  c o m
    Map<V, K> newMap = new HashMap<V, K>();
    for (Map.Entry<K, V> entry : map.entrySet()) {
        newMap.put(entry.getValue(), entry.getKey());
    }
    return newMap;
}

From source file:Main.java

/**
 * Returns map values summary list with unique elements only.
 *
 * @param map map to process//  w  w  w  . j  ava  2s.c  om
 * @param <K> key object type
 * @param <V> value object type
 * @return map values summary list with unique elements only
 */
public static <K, V> ArrayList<V> valuesSummaryList(final Map<K, List<V>> map) {
    final ArrayList<V> summary = new ArrayList<V>(0);
    for (final Map.Entry<K, List<V>> entry : map.entrySet()) {
        final List<V> list = entry.getValue();
        summary.ensureCapacity(summary.size() + list.size());
        for (final V value : list) {
            if (!summary.contains(value)) {
                summary.add(value);
            }
        }
    }
    return summary;
}

From source file:Main.java

public static <K> void merge(Map<K, Float> map1, Map<K, Float> map2) {
    if (map1 == null || isEmpty(map2)) {
        return;/*w w w.  ja  va  2  s  . c o  m*/
    }

    for (Entry<K, Float> entry : map2.entrySet()) {
        K key = entry.getKey();
        Float value = entry.getValue();
        Float value2 = map1.get(key);

        if (value2 != null) {
            value += value2;
        }
        map1.put(key, value);
    }
}

From source file:Main.java

private static Integer calculateDotProduct(Map<Object, Integer> map1, Map<Object, Integer> map2) {
    Integer sum = 0;//w  w w . j  a  v a  2  s . c o  m
    for (Entry<Object, Integer> entry1 : map1.entrySet()) {
        Integer value1 = entry1.getValue();
        Integer value2 = map2.get(entry1.getKey());
        if (value2 != null) {
            Integer prod = value1 * value2;
            sum += prod;
        }
    }
    return sum;
}

From source file:Main.java

public static List<NameValuePair> convertParametersToNameValuePairs(final Map<String, String> pParameters) {
    final List<NameValuePair> result = new ArrayList<NameValuePair>();

    for (final Entry<String, String> entry : pParameters.entrySet()) {
        result.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }/*from w  w  w .  j  av  a  2  s  .c om*/

    return result;
}

From source file:Main.java

public static HashMap<String, String> copyMap(Map<String, Object> map) {
    HashMap<String, String> result = new HashMap<String, String>();

    for (Map.Entry<String, Object> entry : map.entrySet()) {
        String value = null;/*from   w w w .jav  a 2 s. c o  m*/

        if (entry.getValue() != null) {
            value = entry.getValue().toString();
        }

        result.put(entry.getKey(), value);
    }

    return result;
}

From source file:Main.java

/**
 * Replaces all dynamic namespaces in a XPath expression with respective prefixes 
 * in namespace map.//w  w  w  . j  a v  a2  s .  c o m
 * 
 * XPath: <code>/{http://sample.org/foo}foo/{http://sample.org/bar}bar</code> 
 * results in <code>/ns1:foo/ns2:bar</code> where the namespace map contains ns1 and ns2.
 * 
 * @param expression
 * @param namespaces
 * @return
 */
public static String replaceDynamicNamespaces(String expression, Map<String, String> namespaces) {
    String expressionResult = expression;

    for (Entry<String, String> namespaceEntry : namespaces.entrySet()) {
        if (expressionResult.contains(DYNAMIC_NS_START + namespaceEntry.getValue() + DYNAMIC_NS_END)) {
            expressionResult = expressionResult.replaceAll("\\" + DYNAMIC_NS_START
                    + namespaceEntry.getValue().replace(".", "\\.") + "\\" + DYNAMIC_NS_END,
                    namespaceEntry.getKey() + ":");
        }
    }

    return expressionResult;
}

From source file:org.ambraproject.wombat.freemarker.ReplaceParametersDirective.java

@VisibleForTesting
static Multimap<String, String> replaceParameters(SimpleHash parameterMap,
        Multimap<String, TemplateModel> replacements) throws TemplateException {
    Multimap<String, String> result = HashMultimap.create();

    // The map is passed in as a Map<String, String[]>, but Freemarker doesn't support generics
    // (and wraps the map in its own data structure).
    Map map = parameterMap.toMap();
    Iterator iter = map.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry entry = (Map.Entry) iter.next();
        String[] values = (String[]) entry.getValue();
        for (String value : values) {
            result.put((String) entry.getKey(), value);
        }/* ww  w. ja  v a  2 s  .c  o m*/
    }

    for (Map.Entry<String, Collection<TemplateModel>> replacementEntry : replacements.asMap().entrySet()) {
        Collection<String> replacementValues = Collections2.transform(replacementEntry.getValue(),
                Object::toString);
        result.replaceValues(replacementEntry.getKey(), replacementValues);
    }

    return ImmutableSetMultimap.copyOf(result);
}