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:Main.java

/**
 * <p>Return subMap from target Map where the key start with appointing keyPrefix, subMap's remove keyPrefix</p>
 * <p>subMap([key1:value1,key21:value21,key22:value22], new HashMap(), key2) return [1:value21,2:value22]</p>
 * @param sourceMap//from w w  w  . j  ava  2s  . co m
 * @param targetMap
 * @param keyPrefix
 * @return
 */
public static <E extends Object> Map<String, E> subMap(Map<String, E> sourceMap, Map<String, E> targetMap,
        String keyPrefix) {
    Set<Map.Entry<String, E>> entrys = sourceMap.entrySet();
    for (Map.Entry<String, E> entry : entrys) {
        String key = entry.getKey();
        if (key != null && key.startsWith(keyPrefix)) {
            targetMap.put(key.replace(keyPrefix, ""), entry.getValue());
        }
    }
    return targetMap;
}

From source file:org.cloudfoundry.identity.uaa.util.UaaHttpRequestUtils.java

public static String paramsToQueryString(Map<String, String[]> parameterMap) {
    return parameterMap.entrySet().stream().flatMap(
            param -> stream(param.getValue()).map(value -> param.getKey() + "=" + encodeParameter(value)))
            .collect(Collectors.joining("&"));
}

From source file:au.edu.anu.portal.portlets.basiclti.support.CollectionsSupport.java

/**
 * Print all key-value pairs of a map to the log at debug level
 * @param map//from   w  w  w  .  ja v  a2 s  .  co  m
 */
public static void printMap(Map<?, ?> map) {
    for (Map.Entry<?, ?> param : map.entrySet()) {
        log.error(param.getKey() + ":" + param.getValue());
    }
}

From source file:Main.java

public static String convert2XML(Map<String, String> params) {
    StringBuffer sb = new StringBuffer("<xml>");
    try {/* w ww  .  j a  v a2s.  co  m*/
        Iterator it = params.entrySet().iterator();
        while (it.hasNext()) {
            Entry entry = (Entry) it.next();
            sb.append("<" + entry.getKey() + ">");
            // sb.append("<![CDATA[" + entry.getValue() + "]]");
            sb.append(entry.getValue());
            sb.append("</" + entry.getKey() + ">");
        }
        sb.append("</xml>");

    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:Main.java

static String map2UrlQueryString(Map<String, Object> map) {
    StringBuilder sb = new StringBuilder();
    for (HashMap.Entry<String, Object> e : map.entrySet()) {
        try {//w ww  .j a v a 2 s .c om
            sb.append(e.getKey());
            sb.append('=');
            sb.append(URLEncoder.encode(String.valueOf(e.getValue()), "UTF-8"));
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        sb.append('&');
    }
    if (sb.length() == 0)
        return "";
    else
        return sb.substring(0, sb.length() - 1);
}

From source file:Main.java

public static <K> Map<K, String> trimMapValue(Map<K, String> map) {
    Map<K, String> covertedResult = new HashMap<K, String>(map.size());

    Set<Entry<K, String>> entrySet = map.entrySet();
    for (Entry<K, String> entry : entrySet) {
        covertedResult.put(entry.getKey(), entry.getValue().trim());
    }// w w w .ja va2  s. c om
    return covertedResult;
}

From source file:Main.java

public static <K, V extends Comparable<? super V>> Map<K, Integer> calculateSum(Map<K, Integer> map) {
    Map<K, Integer> result = new HashMap<K, Integer>();
    for (Map.Entry<K, Integer> entry : map.entrySet()) {
        K key = entry.getKey();/*from  w  ww  .j  a v a 2 s  .co m*/
        Integer value = (Integer) getValue(result, key, Integer.class);
        value += entry.getValue();
        result.put(key, value);
    }

    return result;
}

From source file:Main.java

public static boolean isEmptyMap(Map<String, ?> map) {
    if (map == null || map.isEmpty()) {
        return true;
    }//from  w ww  . ja v  a 2 s .  c o m
    for (Entry<String, ?> entry : map.entrySet()) {
        if (entry.getKey() == null || entry.getValue() == null) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static final String toStringMap(Map map, boolean ln) {
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (Object eo : map.entrySet()) {
        Map.Entry e = (Map.Entry) eo;
        String key = "" + e.getKey();
        String value = "" + e.getValue();
        if (!first) {
            sb.append(", ");
            if (ln)
                sb.append("\n");
        } else {/*from w w w  .j a  v  a  2  s  . c o m*/
            first = false;
        }
        sb.append(key + "=" + value);
    }
    return sb.toString();
}

From source file:Main.java

public static <K, V> Map<V, K> swapKeyValue(Map<K, V> map, Map<V, K> newMap) {
    if (map == null) {
        return null;
    }//from   w ww.j av  a 2  s .  co  m

    for (Map.Entry<K, V> entry : map.entrySet()) {
        newMap.put(entry.getValue(), entry.getKey());
    }
    return newMap;
}