Example usage for java.util HashMap put

List of usage examples for java.util HashMap put

Introduction

In this page you can find the example usage for java.util HashMap put.

Prototype

public V put(K key, V value) 

Source Link

Document

Associates the specified value with the specified key in this map.

Usage

From source file:com.groupon.odo.proxylib.Utils.java

public static HashMap<String, Object> getJQGridJSON(Object[] rows, String rowName) {
    HashMap<String, Object> returnVal = new HashMap<String, Object>();
    returnVal.put("records", rows.length);
    returnVal.put("total", 1);
    returnVal.put("page", 1);
    returnVal.put(rowName, rows);/*  w w w  .  j av  a2 s. c om*/
    return returnVal;
}

From source file:com.semsaas.utils.anyurl.App.java

private static String[] processOption(String[] args, Properties props) {
    LongOpt[] options = new LongOpt[] { new LongOpt("output", LongOpt.REQUIRED_ARGUMENT, null, 'o') };

    // Build auxilary structures
    HashMap<Integer, LongOpt> shortOptionMap = new HashMap<Integer, LongOpt>();
    StringBuffer decl = new StringBuffer();
    for (LongOpt o : options) {
        shortOptionMap.put(o.getVal(), o);
        decl.append((char) o.getVal());
        if (o.getHasArg() == LongOpt.OPTIONAL_ARGUMENT) {
            decl.append("::");
        } else if (o.getHasArg() == LongOpt.REQUIRED_ARGUMENT) {
            decl.append(":");
        }//from ww  w . j a v  a  2 s . co  m
    }
    Getopt g = new Getopt("anyurl", args, decl.toString(), options);

    int c = 0;
    while ((c = g.getopt()) != -1) {
        LongOpt opt = shortOptionMap.get(c);
        String optName = opt.getName();
        String optVal = g.getOptarg();
        props.put(optName, optVal);
    }

    // NB: Getopt moves non options to the end
    return Arrays.copyOfRange(args, g.getOptind(), args.length);
}

From source file:com.zimbra.cs.account.ExtAuthTokenKey.java

private static synchronized void refresh(boolean reload) throws ServiceException {
    Provisioning prov = Provisioning.getInstance();
    Config config = prov.getConfig();//from w  w w.ja v  a  2s.c o m
    // force reload
    if (reload)
        prov.reload(config);

    String key = config.getAttr(Provisioning.A_zimbraExternalAccountProvisioningKey);

    if (StringUtil.isNullOrEmpty(key)) {
        prov.reload(config);
        key = config.getAttr(Provisioning.A_zimbraExternalAccountProvisioningKey);
    }

    // bootstrap. automatically create new random key
    if (StringUtil.isNullOrEmpty(key)) {
        ExtAuthTokenKey extAuthkey = new ExtAuthTokenKey(0, null);
        HashMap<String, String> attrs = new HashMap<String, String>();
        attrs.put(Provisioning.A_zimbraExternalAccountProvisioningKey, extAuthkey.getEncoded());
        Provisioning.getInstance().modifyAttrs(config, attrs);
        key = config.getAttr(Provisioning.A_zimbraExternalAccountProvisioningKey);
    }

    ExtAuthTokenKey extAuthKey = keyCache.get(key);
    if (extAuthKey == null) {
        extAuthKey = new ExtAuthTokenKey(key);
        keyCache.put(key, extAuthKey);
        keyCache.put(Long.toString(extAuthKey.version), extAuthKey);
        if (latestExtAuthKey == null || latestExtAuthKey.version < latestExtAuthKey.version)
            latestExtAuthKey = extAuthKey;
    }
}

From source file:com.zimbra.cs.account.TrustedTokenKey.java

private static synchronized void refresh(boolean reload) throws ServiceException {
    Provisioning prov = Provisioning.getInstance();
    Config config = prov.getConfig();//from  ww  w  . j  a  v a 2  s.co m
    // force reload
    if (reload)
        prov.reload(config);

    String[] keys = config.getMultiAttr(Provisioning.A_zimbraTwoFactorAuthTrustedDeviceTokenKey);

    if (keys.length == 0) {
        prov.reload(config);
        keys = config.getMultiAttr(Provisioning.A_zimbraTwoFactorAuthTrustedDeviceTokenKey);
    }

    // bootstrap. automatically create new random key
    if (keys.length == 0) {
        TrustedTokenKey key = new TrustedTokenKey(0, null);
        HashMap<String, String> attrs = new HashMap<String, String>();
        attrs.put(Provisioning.A_zimbraTwoFactorAuthTrustedDeviceTokenKey, key.getEncoded());
        Provisioning.getInstance().modifyAttrs(config, attrs);
        keys = config.getMultiAttr(Provisioning.A_zimbraTwoFactorAuthTrustedDeviceTokenKey);
    }

    for (int i = 0; i < keys.length; i++) {
        TrustedTokenKey key = mCache.get(keys[i]);
        if (key == null) {
            key = new TrustedTokenKey(keys[i]);
            mCache.put(keys[i], key);
            mCache.put(Long.toString(key.mVersion), key);
            if (sLatestKey == null || sLatestKey.mVersion < key.mVersion)
                sLatestKey = key;
        }
    }
}

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://from   w w w.  ja v  a  2s .  co 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:Main.java

/**
 * Returns a {@link Map} mapping each unique element in
 * the given {@link Collection} to an {@link Integer}
 * representing the number of occurances of that element
 * in the {@link Collection}./* w  w  w . j av  a2 s .c om*/
 * An entry that maps to <tt>null</tt> indicates that the
 * element does not appear in the given {@link Collection}.
 * @param col The collection to count cardinalities for
 * @return A map of counts, indexed on each element in the collection
 */
public static <E> Map<E, Integer> getCardinalityMap(final Collection<E> col) {
    HashMap<E, Integer> count = new HashMap<E, Integer>();
    for (E obj : col) {
        Integer c = count.get(obj);
        if (null == c) {
            count.put(obj, 1);
        } else {
            count.put(obj, c + 1);
        }
    }
    return count;
}

From source file:gov.nist.healthcare.ttt.webapp.api.GetCCDAFolderTest.java

public static void buildJson3(HashMap<String, Object> json, String[] path) {
    if (path.length == 1) {
        HashMap<String, Object> newObj = new HashMap<>();
        newObj.put("dirs", new ArrayList<HashMap<String, Object>>());
        newObj.put("files", new ArrayList<HashMap<String, Object>>());
        json.put(path[0], newObj);/*from w w  w  . ja va2  s .  c  om*/

    } else {
        HashMap<String, Object> current = (HashMap<String, Object>) json.get(path[0]);
        for (int i = 1; i < path.length; i++) {
            String currentName = path[i];
            if (Pattern.matches(extensionRegex, currentName)) {
                HashMap<String, Object> newFile = new HashMap<>();
                newFile.put("name", currentName);
                newFile.put("link", getLink(path));
                List filesList = (List) current.get("files");
                filesList.add(newFile);
            } else {
                if (containsName((List<Map>) current.get("dirs"), currentName)) {
                    List<Map> directories = (List<Map>) current.get("dirs");
                    current = (HashMap<String, Object>) directories.get(getObjByName(directories, currentName));
                } else {
                    HashMap<String, Object> newObj = new HashMap<>();
                    newObj.put("name", currentName);
                    newObj.put("dirs", new ArrayList<HashMap<String, Object>>());
                    newObj.put("files", new ArrayList<HashMap<String, Object>>());
                    List dirsList = (List) current.get("dirs");
                    dirsList.add(newObj);
                }
            }
        }
    }
}

From source file:Main.java

public static HashMap<String, String> getNodeAttributesMap(Node node) {
    HashMap<String, String> map = new HashMap<String, String>();

    NamedNodeMap namedNodeMap = node.getAttributes();
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Node attribute = namedNodeMap.item(i);
        map.put(attribute.getNodeName(), attribute.getNodeValue());
    }/*from w  w w.ja v a 2s  .c o  m*/

    return map;
}

From source file:org.dojotoolkit.zazl.internal.XMLHttpRequestUtils.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static String xhrRequest(String shrDataString) {
    InputStream is = null;/*from w ww  . ja v  a  2 s.c o m*/
    String json = null;

    try {
        logger.logp(Level.FINER, XMLHttpRequestUtils.class.getName(), "xhrRequest",
                "shrDataString [" + shrDataString + "]");

        Map<String, Object> xhrData = (Map<String, Object>) JSONParser.parse(new StringReader(shrDataString));
        String url = (String) xhrData.get("url");
        String method = (String) xhrData.get("method");
        List headers = (List) xhrData.get("headers");
        URL requestURL = createURL(url);
        URI uri = new URI(requestURL.toString());

        HashMap httpMethods = new HashMap(7);
        httpMethods.put("DELETE", new HttpDelete(uri));
        httpMethods.put("GET", new HttpGet(uri));
        httpMethods.put("HEAD", new HttpHead(uri));
        httpMethods.put("OPTIONS", new HttpOptions(uri));
        httpMethods.put("POST", new HttpPost(uri));
        httpMethods.put("PUT", new HttpPut(uri));
        httpMethods.put("TRACE", new HttpTrace(uri));
        HttpUriRequest request = (HttpUriRequest) httpMethods.get(method.toUpperCase());

        if (request.equals(null)) {
            throw new Error("SYNTAX_ERR");
        }

        for (Object header : headers) {
            StringTokenizer st = new StringTokenizer((String) header, ":");
            String name = st.nextToken();
            String value = st.nextToken();
            request.addHeader(name, value);
        }

        HttpClient client = new DefaultHttpClient();

        HttpResponse response = client.execute(request);
        Map headerMap = new HashMap();

        HeaderIterator headerIter = response.headerIterator();

        while (headerIter.hasNext()) {
            Header header = headerIter.nextHeader();
            headerMap.put(header.getName(), header.getValue());
        }

        int status = response.getStatusLine().getStatusCode();
        String statusText = response.getStatusLine().toString();

        is = response.getEntity().getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        StringBuffer sb = new StringBuffer();

        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append('\n');
        }
        Map m = new HashMap();
        m.put("status", new Integer(status));
        m.put("statusText", statusText);
        m.put("responseText", sb.toString());
        m.put("headers", headerMap.toString());
        StringWriter w = new StringWriter();
        JSONSerializer.serialize(w, m);
        json = w.toString();
        logger.logp(Level.FINER, XMLHttpRequestUtils.class.getName(), "xhrRequest", "json [" + json + "]");
    } catch (Throwable e) {
        logger.logp(Level.SEVERE, XMLHttpRequestUtils.class.getName(), "xhrRequest",
                "Failed request for [" + shrDataString + "]", e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
    return json;
}

From source file:Main.java

public static HashMap<String, String> getMapForJson(String jsonStr) {
    JSONObject jsonObject;//from w  w w  .  j  ava  2 s. c om
    try {
        jsonObject = new JSONObject(jsonStr);

        Iterator<String> keyIter = jsonObject.keys();
        String key;
        Object value;
        HashMap<String, String> valueMap = new HashMap<String, String>();
        while (keyIter.hasNext()) {
            key = keyIter.next();
            value = jsonObject.get(key);
            valueMap.put(key, String.valueOf(value));
        }
        return valueMap;
    } catch (Exception e) {
        e.printStackTrace();
        // Log.e(HttpClientUtils.TAG, e.toString());
    }
    return null;
}