Example usage for java.util Map keySet

List of usage examples for java.util Map keySet

Introduction

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

Prototype

Set<K> keySet();

Source Link

Document

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

Usage

From source file:Maps.java

public static <K, V> Map<K, V> putAll(Map<K, V> map, Map<K, V> toAdd) {
    switch (toAdd.size()) {
    case 0:// w w  w.  ja  v  a  2s .c o  m
        // No-op.
        return map;
    case 1: {
        // Add one element.
        K key = toAdd.keySet().iterator().next();
        return put(map, key, toAdd.get(key));
    }
    default:
        // True list merge, result >= 2.
        switch (map.size()) {
        case 0:
            return new HashMap<K, V>(toAdd);
        case 1: {
            HashMap<K, V> result = new HashMap<K, V>();
            K key = map.keySet().iterator().next();
            result.put(key, map.get(key));
            result.putAll(toAdd);
            return normalize(result);
        }
        default:
            map.putAll(toAdd);
            return map;
        }
    }
}

From source file:com.keybox.common.util.AppConfig.java

/**
 * gets the property from config and replaces placeholders
 *
 * @param name           property name/*from w ww .j a v  a 2  s .  c  o  m*/
 * @param replacementMap name value pairs of place holders to replace
 * @return configuration property
 */
public static String getProperty(String name, Map<String, String> replacementMap) {

    String value = prop.getString(name);
    if (StringUtils.isNotEmpty(value)) {
        //iterate through map to replace text
        Set<String> keySet = replacementMap.keySet();
        for (String key : keySet) {
            //replace values in string
            String rVal = replacementMap.get(key);
            value = value.replace("${" + key + "}", rVal);
        }
    }
    return value;
}

From source file:fr.liglab.adele.cilia.workbench.restmonitoring.utils.http.CiliaRestHelper.java

public static List<StateVar> getStateVar(PlatformID platformID, String chainName, String compoName)
        throws CiliaException {
    List<StateVar> retval = new ArrayList<StateVar>();
    Map<String, Boolean> list = getStateVarList(platformID, chainName, compoName);

    for (String name : list.keySet()) {
        boolean enabled = list.get(name);
        String value = null;/*from ww  w  . ja va2  s.co m*/
        try {
            value = getStateVarValue(platformID, chainName, compoName, name);
        } catch (Exception e) {
            value = "<<not available>>";
        }
        retval.add(new StateVar(name, enabled, value));
    }

    return retval;
}

From source file:com.tlabs.eve.HttpClientTest.java

public static List<NameValuePair> toNameValuePair(Map<String, String> params) {
    List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
    for (String p : params.keySet()) {
        pairs.add(new BasicNameValuePair(p, params.get(p)));
    }/*from   www. ja  va2 s .  co  m*/
    return pairs;
}

From source file:com.adguard.compiler.LocaleUtils.java

public static void convertFromChromeToFirefoxLocales(File chromeLocalesDir) throws IOException {

    for (File file : chromeLocalesDir.listFiles()) {

        File chromeLocaleFile = new File(file, "messages.json");
        if (!SupportedLocales.supported(file.getName())) {
            FileUtils.deleteQuietly(file);
            continue;
        }/*from w w w .  j a va  2s  .com*/

        String firefoxLocale = StringUtils.replace(file.getName(), "_", "-");
        File appLocaleFile = new File(chromeLocalesDir, firefoxLocale + ".properties");

        byte[] content = FileUtils.readFileToByteArray(chromeLocaleFile);
        Map map = objectMapper.readValue(content, Map.class);
        StringBuilder sb = new StringBuilder();
        for (Object key : map.keySet()) {
            String message = (String) ((Map) map.get(key)).get("message");
            message = message.replaceAll("\n", "\\\\n");
            sb.append(key).append("=").append(message);
            sb.append(System.lineSeparator());
        }
        FileUtils.writeStringToFile(appLocaleFile, sb.toString(), "utf-8");
        FileUtils.deleteQuietly(file);
    }
}

From source file:com.mockey.ui.Util.java

@SuppressWarnings("unchecked")
public static void saveErrorMap(Map errorMap, HttpServletRequest req) {
    if (errorMap != null) {
        Iterator<String> iter = errorMap.keySet().iterator();
        while (iter.hasNext()) {
            String key = (String) iter.next();
            String value = (String) errorMap.get(key);
            save((key + " : " + value), ERROR, req);
        }/*from w w  w  .  j  av a2  s.c  o  m*/
    }

}

From source file:com.roncoo.utils.MerchantApiUtil.java

/**
 * ?HTML?//from   w  w w.j  a v  a 2 s. c  o m
 * @param sParaTemp ?
 * @param strMethod ????post?get
 * @param strButtonName 
 * @return ???HTML
 */
public static String buildRequest(Map<String, Object> sParaTemp, String strMethod, String strButtonName,
        String actionUrl) {
    //?
    List<String> keys = new ArrayList<String>(sParaTemp.keySet());
    StringBuffer sbHtml = new StringBuffer();

    sbHtml.append("<form id=\"rppaysubmit\" name=\"rppaysubmit\" action=\"" + actionUrl + "\" method=\""
            + strMethod + "\">");

    for (int i = 0; i < keys.size(); i++) {
        String name = (String) keys.get(i);
        Object object = sParaTemp.get(name);
        String value = "";

        if (object != null) {
            value = (String) sParaTemp.get(name);
        }

        sbHtml.append("<input type=\"hidden\" name=\"" + name + "\" value=\"" + value + "\"/>");
    }

    //submit???name
    sbHtml.append("<input type=\"submit\" value=\"" + strButtonName + "\" style=\"display:none;\"></form>");
    sbHtml.append("<script>document.forms['rppaysubmit'].submit();</script>");

    return sbHtml.toString();
}

From source file:Main.java

/**
 * Converts a map to XML, where parent is the parent element, and every
 * other element is of the form <key>value</key> or <listElementName>listvalue</listElementName>
 * for each list index.  The list elements MUST be Strings if they are to
 * have text nodes.  But, they may also be another Map with key/value
 * pairs.// ww  w . j a va2  s .  c o m
 * <p/>
 * We assume that every key is an actual XML compatible element name; i.e. a
 * String object.  The values will be XML encoded automatically.
 *
 * @param elements        the elements, whether a list of elements or a map
 *                        of key/value pairs
 * @param parentElement   the parent element to append the new child to
 * @param document        the XML document to create new elements in
 * @param listElementName the name for the element, for every element in the
 *                        List
 *
 * @throws ParserConfigurationException if a configuration error occurs
 */
public static void mapToNode(final Object elements, final Element parentElement, final Document document,
        final String listElementName) throws ParserConfigurationException {
    Object value;
    if (elements instanceof Map) {
        final Map map = (Map) elements;
        final Iterator it = map.keySet().iterator();
        Element tmp;

        while (it.hasNext()) {
            final String key = (String) it.next();

            value = map.get(key);
            if (value instanceof Map) {
                tmp = document.createElement(key);
                mapToNode(value, tmp, document, null);
                parentElement.appendChild(tmp);
            } else if (value instanceof List) {
                mapToNode(value, parentElement, document, key);
            } else {
                tmp = document.createElement(key);
                if (value != null) { // null elements don't get in
                    tmp.appendChild(document.createTextNode((String) value));
                    parentElement.appendChild(tmp);
                }
            }
        }
    } else if (elements instanceof List) {
        if (listElementName == null || "".equals(listElementName.trim())) {
            throw new IllegalArgumentException(
                    "listElementName can never be null if a list is passed " + "in for elements");
        }
        final List list = (List) elements;
        for (int index = 0; index < list.size(); index++) {
            final Object element = list.get(index);
            if (element instanceof String) { // text node
                final String text = (String) list.get(index);
                final Element tmp = document.createElement(listElementName);
                tmp.appendChild(document.createTextNode(text));
                parentElement.appendChild(tmp);
            } else if (element instanceof Map) { // sub elements that have key/value pairs, or key/List pairs
                final Element tmp = document.createElement(listElementName);
                parentElement.appendChild(tmp);
                mapToNode(element, tmp, document, null);
            } else if (element instanceof List) {
                throw new IllegalArgumentException(
                        "List not supported " + "inside of List, cannot determine element name");
            }
        }
    } else {
        throw new IllegalArgumentException("unsupported class type for " + "mapToXML");
    }
}

From source file:ch.rasc.wampspring.message.BaseMessageTest.java

@SuppressWarnings({ "rawtypes", "unchecked" })
static String toJsonArray(Object... arguments) {
    StringBuilder sb = new StringBuilder();
    sb.append("[");

    for (Object argument : arguments) {
        if (sb.length() > 1) {
            sb.append(",");
        }// w w w .  j  a  v  a  2  s .  c o m
        if (argument != null) {
            if (argument instanceof Number) {
                sb.append(argument);
            } else if (argument instanceof Boolean) {
                sb.append(argument);
            } else if (argument instanceof String) {
                sb.append("\"").append(argument).append("\"");
            } else if (argument instanceof List) {
                sb.append("[");
                boolean first = true;
                for (Object entry : (List) argument) {
                    if (!first) {
                        sb.append(",");
                    } else {
                        first = false;
                    }
                    if (entry instanceof String) {
                        sb.append("\"").append(entry).append("\"");
                    } else {
                        sb.append(entry);
                    }
                }
                sb.append("]");
            } else if (argument instanceof Map) {
                Map<String, Object> map = (Map<String, Object>) argument;
                sb.append("{");
                boolean first = true;
                for (String key : map.keySet()) {
                    if (!first) {
                        sb.append(",");
                    } else {
                        first = false;
                    }
                    sb.append("\"").append(key).append("\"");
                    sb.append(":");
                    Object value = map.get(key);
                    if (value instanceof String) {
                        sb.append("\"").append(value).append("\"");
                    } else {
                        sb.append(value);
                    }
                }
                sb.append("}");
            }
        } else {
            sb.append("null");
        }
    }

    sb.append("]");
    return sb.toString();
}

From source file:com.cubeia.backoffice.report.RequestBean.java

public static RequestBean parse(HttpServletRequest req) {
    /*/*  w w w .j a v  a 2  s.com*/
     * Trim the path (which becomes the report name)
     */
    String path = trim(req.getPathInfo());
    /*
     * check the format and do default if needed
     */
    String form = req.getParameter("format");
    if (form == null) {
        form = Format.CSV.name();
    }
    /*
     * Convert parameter map to a simple string-string map
     */
    @SuppressWarnings("rawtypes")
    Map params = req.getParameterMap();
    Map<String, String> tmp = new HashMap<String, String>(params.size());
    for (Object key : params.keySet()) {
        tmp.put(key.toString(), req.getParameter(key.toString()));
    }
    /*
     * Continue parsing
     */
    return parse(path, tmp, form);
}