Java Code Examples for java.util.HashMap.putIfAbsent()

The following are Jave code examples for showing how to use putIfAbsent() of the java.util.HashMap class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: elasticsearch_my   File: ClientYamlTestExecutionContext.java   View Source Code Vote up 6 votes
/**
 * Calls an elasticsearch api with the parameters and request body provided as arguments.
 * Saves the obtained response in the execution context.
 */
public ClientYamlTestResponse callApi(String apiName, Map<String, String> params, List<Map<String, Object>> bodies,
                                Map<String, String> headers) throws IOException {
    //makes a copy of the parameters before modifying them for this specific request
    HashMap<String, String> requestParams = new HashMap<>(params);
    requestParams.putIfAbsent("error_trace", "true"); // By default ask for error traces, this my be overridden by params
    for (Map.Entry<String, String> entry : requestParams.entrySet()) {
        if (stash.containsStashedValue(entry.getValue())) {
            entry.setValue(stash.getValue(entry.getValue()).toString());
        }
    }

    HttpEntity entity = createEntity(bodies, headers);
    try {
        response = callApiInternal(apiName, requestParams, entity, headers);
        return response;
    } catch(ClientYamlTestResponseException e) {
        response = e.getRestTestResponse();
        throw e;
    } finally {
        // if we hit a bad exception the response is null
        Object responseBody = response != null ? response.getBody() : null;
        //we always stash the last response body
        stash.stashValue("body", responseBody);
    }
}
 
Example 2
Project: HackerRank_solutions   File: Solution.java   View Source Code Vote up 5 votes
public static void buyIceCream(int [] costs, int money) {
    HashMap<Integer, Integer> map = new HashMap<>(); // key = cost, value = ice cream ID
    for (int i = 0; i < costs.length; i++) {
        int icecreamID = i + 1;
        int cost       = costs[i];
        
        /* Find 2 flavors to buy that sum to "money" */
        int otherCost  = money - cost;
        if (map.containsKey(otherCost)) {
            System.out.println(map.get(otherCost) + " " + icecreamID);
        }
        
        map.putIfAbsent(cost, icecreamID);
    }
}
 
Example 3
Project: HackerRank_solutions   File: Solution.java   View Source Code Vote up 5 votes
public static void buyIceCream(int [] costs, int money) {
    HashMap<Integer, Integer> map = new HashMap<>(); // key = cost, value = ice cream ID
    for (int i = 0; i < costs.length; i++) {
        int icecreamID = i + 1;
        int cost       = costs[i];
        
        /* Find 2 flavors to buy that sum to "money" */
        int otherCost  = money - cost;
        if (map.containsKey(otherCost)) {
            System.out.println(map.get(otherCost) + " " + icecreamID);
        }
        
        map.putIfAbsent(cost, icecreamID);
    }
}
 
Example 4
Project: BUILD_file_generator   File: ReferencedClassesParser.java   View Source Code Vote up 5 votes
/**
 * Returns an immutable set containing each of elements, minus duplicates, in the order each
 * appears first in the source collection.
 *
 * <p>Comparison (equals() and hashCode()) are performed on key.apply(X) instead of on X.
 */
private <T, S> ImmutableSet<T> distinctByPredicate(
    Iterable<? extends T> items, Function<T, S> key) {
  HashMap<S, T> m = new HashMap<>();
  for (T item : items) {
    m.putIfAbsent(key.apply(item), item);
  }
  return ImmutableSet.copyOf(m.values());
}
 
Example 5
Project: ACE_HackerRank   File: Solution.java   View Source Code Vote up 5 votes
public static void buyIceCream(int [] costs, int money) {
    HashMap<Integer, Integer> map = new HashMap<>(); // key = cost, value = ice cream ID
    for (int i = 0; i < costs.length; i++) {
        int icecreamID = i + 1;
        int cost       = costs[i];
        
        /* Find 2 flavors to buy that sum to "money" */
        int otherCost  = money - cost;
        if (map.containsKey(otherCost)) {
            System.out.println(map.get(otherCost) + " " + icecreamID);
        }
        
        map.putIfAbsent(cost, icecreamID);
    }
}
 
Example 6
Project: ACE_HackerRank   File: Solution.java   View Source Code Vote up 5 votes
public static void buyIceCream(int [] costs, int money) {
    HashMap<Integer, Integer> map = new HashMap<>(); // key = cost, value = ice cream ID
    for (int i = 0; i < costs.length; i++) {
        int icecreamID = i + 1;
        int cost       = costs[i];
        
        /* Find 2 flavors to buy that sum to "money" */
        int otherCost  = money - cost;
        if (map.containsKey(otherCost)) {
            System.out.println(map.get(otherCost) + " " + icecreamID);
        }
        
        map.putIfAbsent(cost, icecreamID);
    }
}