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:kafka.benchmark.AdvertisingTopology.java

@SuppressWarnings("unchecked")
private static String getZookeeperServers(Map<?, ?> conf) {
    if (!conf.containsKey("zookeeper.servers")) {
        throw new IllegalArgumentException("Not zookeeper servers found!");
    }//from   w w  w.j a v a 2 s  . c  om
    return listOfStringToString((List<String>) conf.get("zookeeper.servers"),
            String.valueOf(conf.get("zookeeper.port")));
}

From source file:Main.java

/**
 * Combines 2 maps by just adding the values of the same key together.
 *
 * @param a A map//w w  w  . ja  v a  2s  .  com
 * @param b Another map
 * @param combiner The combiner of the values
 * @param <K> The type of the keys
 * @param <V> The type of the values
 *
 * @return a map that contains all the key of map a and b and the combined values.
 */
public static <K, V> Map<K, V> combine(Map<? extends K, ? extends V> a, Map<? extends K, ? extends V> b,
        BinaryOperator<V> combiner) {
    Map<K, V> result = new HashMap<>();

    for (Entry<? extends K, ? extends V> entry : a.entrySet()) {
        if (b.containsKey(entry.getKey()))
            result.put(entry.getKey(), combiner.apply(entry.getValue(), b.get(entry.getKey())));
        else
            result.put(entry.getKey(), entry.getValue());
    }

    b.entrySet().stream().filter(e -> !result.containsKey(e.getKey()))
            .forEach(e -> result.put(e.getKey(), e.getValue()));

    return result;
}

From source file:com.taobao.diamond.common.Constants.java

/**
 * field??fromField/* w ww  . ja v  a  2  s. c  o  m*/
 * @param old Map&lt;??, &gt;
 * @param field ??
 * @param fromField ???
 */
private static void setValue(Map<String, Object> old, String field, String fromField) {
    if (old.containsKey(field))
        return; //?

    try {
        Constants.class.getDeclaredField(field).set(Constants.class,
                Constants.class.getDeclaredField(fromField).get(Constants.class));
    } catch (IllegalAccessException | NoSuchFieldException e) {
        throw new IllegalArgumentException("" + field + ", " + fromField, e);
    }
}

From source file:com.sugaronrest.restapicalls.methodcalls.Authentication.java

/**
 * Login to SugarCRM via REST API call./*from  w w  w . j  a v a2  s  . co  m*/
 *
 *  @param loginRequest LoginRequest object.
 *  @return LoginResponse object.
 */
public static LoginResponse login(LoginRequest loginRequest) throws Exception {

    LoginResponse loginResponse = new LoginResponse();

    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        String passwordHash = new BigInteger(1, md5.digest(loginRequest.password.getBytes())).toString(16);

        Map<String, String> userCredentials = new LinkedHashMap<String, String>();
        userCredentials.put("user_name", loginRequest.username);
        userCredentials.put("password", passwordHash);

        Map<String, Object> auth = new LinkedHashMap<String, Object>();
        auth.put("user_auth", userCredentials);
        auth.put("application_name", "RestClient");

        ObjectMapper mapper = new ObjectMapper();
        String jsonAuthData = mapper.writeValueAsString(auth);

        Map<String, Object> request = new LinkedHashMap<String, Object>();
        request.put("method", "login");
        request.put("input_type", "json");
        request.put("response_type", "json");
        request.put("rest_data", auth);

        String jsonRequest = mapper.writeValueAsString(request);

        request.put("rest_data", jsonAuthData);

        HttpResponse response = Unirest.post(loginRequest.url).fields(request).asString();

        String jsonResponse = response.getBody().toString();
        loginResponse.setJsonRawRequest(jsonRequest);
        loginResponse.setJsonRawResponse(jsonResponse);

        Map<String, Object> responseObject = mapper.readValue(jsonResponse, Map.class);
        if (responseObject.containsKey("id")) {
            loginResponse.sessionId = (responseObject.get("id").toString());
            loginResponse.setStatusCode(response.getStatus());
            loginResponse.setError(null);
        } else {
            ErrorResponse errorResponse = mapper.readValue(jsonResponse, ErrorResponse.class);
            errorResponse.setStatusCode(HttpStatus.SC_BAD_REQUEST);
            loginResponse.setStatusCode(HttpStatus.SC_BAD_REQUEST);
            loginResponse.setError(errorResponse);
        }
    } catch (Exception exception) {
        ErrorResponse errorResponse = ErrorResponse.format(exception, exception.getMessage());
        loginResponse.setError(errorResponse);
    }

    return loginResponse;
}

From source file:kafka.benchmark.AdvertisingTopology.java

@SuppressWarnings("unchecked")
private static String getKafkaBrokers(Map<?, ?> conf) {
    if (!conf.containsKey("kafka.brokers")) {
        throw new IllegalArgumentException("No kafka brokers found!");
    }//from w ww .ja  v  a 2  s.  c o  m
    if (!conf.containsKey("kafka.port")) {
        throw new IllegalArgumentException("No kafka port found!");
    }
    return listOfStringToString((List<String>) conf.get("kafka.brokers"),
            String.valueOf(conf.get("kafka.port")));
}

From source file:at.gv.egiz.bku.binding.HttpUtil.java

/**
 * Extracts charset from a content type header.
 * /*from   w  ww .  j  a va 2s .  c om*/
 * @param contentType
 * @param replaceNullWithDefault
 *          if true the method return the default charset if not set
 * @return charset String or null if not present
 */
public static String getCharset(String contentType, boolean replaceNullWithDefault) {
    ParameterParser pf = new ParameterParser();
    pf.setLowerCaseNames(true);
    Map<?, ?> map = pf.parse(contentType, SEPARATOR);
    String retVal = (String) map.get(CHAR_SET);
    if ((retVal == null) && (replaceNullWithDefault)) {
        if (map.containsKey(APPLICATION_URL_ENCODED)) {
            // default charset for url encoded data
            return "UTF-8";
        }
        retVal = getDefaultCharset();
    }
    return retVal;
}

From source file:com.omertron.rottentomatoesapi.tools.ApiBuilder.java

/**
 * Get and process the URL from the properties map
 *
 * @param properties//from   w w w.ja  va2  s  .  c  om
 * @return The processed URL
 * @throws RottenTomatoesException
 */
private static String getUrlFromProps(Map<String, String> properties) throws RottenTomatoesException {
    if (properties.containsKey(PROPERTY_URL) && StringUtils.isNotBlank(properties.get(PROPERTY_URL))) {
        String url = properties.get(PROPERTY_URL);

        // If we have the ID, then we need to replace the "{movie-id}" in the URL
        if (properties.containsKey(PROPERTY_ID) && StringUtils.isNotBlank(properties.get(PROPERTY_ID))) {
            url = url.replace(MOVIE_ID, String.valueOf(properties.get(PROPERTY_ID)));
            // We don't need this property anymore
            properties.remove(PROPERTY_ID);
        }
        // We don't need this property anymore
        properties.remove(PROPERTY_URL);
        return url;
    } else {
        throw new RottenTomatoesException(ApiExceptionType.INVALID_URL, "No URL specified");
    }
}

From source file:com.vmware.bdd.plugin.ambari.utils.AmUtils.java

public static List<Map<String, Object>> toAmConfigurations(List<Map<String, Object>> configurations,
        String configurationType, Map<String, String> property) {
    if (configurations == null) {
        configurations = new ArrayList<Map<String, Object>>();
    }//www.j a  v  a 2 s .  c o m
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(configurationType, property);
    if (configurations.isEmpty()) {
        configurations.add(configuration);
    } else {
        boolean isContainsKey = false;
        for (Map<String, Object> nodeConfiguration : configurations) {
            if (nodeConfiguration.containsKey(configurationType)) {
                Map<String, String> properties = (Map<String, String>) nodeConfiguration.get(configurationType);
                properties.putAll(property);
                isContainsKey = true;
            }
        }
        if (!isContainsKey) {
            configurations.add(configuration);
        }
    }
    return configurations;
}

From source file:fr.esiea.windmeal.fill.database.OwnerAndProviderImportationTest.java

private static Set<Tag> getTagsFromMap(Map<String, String> dataMap) {
    int acc = 1;//from  w  ww  .  jav  a2s.c om

    Set<Tag> tags = new HashSet<Tag>();
    while (dataMap.containsKey("tag" + acc)) {
        tags.add(Tag.valueOf(dataMap.get("tag" + acc)));
        acc++;

    }
    return tags;
}

From source file:org.apache.streams.elasticsearch.ElasticsearchMetadataUtil.java

public static String getType(Map<String, Object> metadata, ElasticsearchWriterConfiguration config) {

    String type = null;/*from   w  ww . j  a va  2s.c o m*/

    if (metadata != null && metadata.containsKey("type"))
        type = (String) metadata.get("type");

    if (type == null || (config.getForceUseConfig() != null && config.getForceUseConfig())) {
        type = config.getType();
    }

    return type;
}