Example usage for java.util Map putIfAbsent

List of usage examples for java.util Map putIfAbsent

Introduction

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

Prototype

default V putIfAbsent(K key, V value) 

Source Link

Document

If the specified key is not already associated with a value (or is mapped to null ) associates it with the given value and returns null , else returns the current value.

Usage

From source file:Main.java

public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();

    for (int i = 0; i < 10; i++) {
        map.putIfAbsent(i, "val" + i);
    }/*  w  ww .j  av a 2 s.  c  o m*/

    map.forEach((id, val) -> System.out.println(val));
}

From source file:Main.java

public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();

    for (int i = 0; i < 10; i++) {
        map.putIfAbsent(i, "val" + i);
    }//from w  w w.  j av a  2s .  co m

    map.forEach((id, val) -> System.out.println(val));

    System.out.println(map.getOrDefault(42, "not found")); // not found
}

From source file:Main.java

public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();

    for (int i = 0; i < 10; i++) {
        map.putIfAbsent(i, "val" + i);
    }/*w w w . ja v a  2 s .c  o  m*/

    map.forEach((id, val) -> System.out.println(val));

    map.remove(3, "val3");
    System.out.println(map.get(3)); // val33

    map.remove(3, "val33");
    System.out.println(map.get(3)); // null
}

From source file:Main.java

public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();

    for (int i = 0; i < 10; i++) {
        map.putIfAbsent(i, "val" + i);
    }//from   w  w  w.  j a v a2 s .co  m

    map.forEach((id, val) -> System.out.println(val));

    map.merge(9, "val9", (value, newValue) -> value.concat(newValue));
    System.out.println(map.get(9)); // val9

    map.merge(9, "concat", (value, newValue) -> value.concat(newValue));
    System.out.println(map.get(9)); // val9concat
}

From source file:Main.java

public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();

    for (int i = 0; i < 10; i++) {
        map.putIfAbsent(i, "val" + i);
    }/*from   ww w . j  a va 2s  . c  o m*/

    map.forEach((id, val) -> System.out.println(val));

    map.computeIfPresent(3, (num, val) -> val + num);
    System.out.println(map.get(3)); // val33

    map.computeIfPresent(9, (num, val) -> null);
    System.out.println(map.containsKey(9)); // false

    map.computeIfAbsent(23, num -> "val" + num);
    System.out.println(map.containsKey(23)); // true

    map.computeIfAbsent(3, num -> "bam");
    System.out.println(map.get(3)); // val33
}

From source file:Main.java

public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
    Map<Object, Boolean> seen = new ConcurrentHashMap<>();
    return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

From source file:Main.java

static void appendProp(Map<String, Object> properties, String key, Object valueToAppend) {
    properties.putIfAbsent(key, new LinkedHashSet<String>());
    ((Collection<String>) properties.get(key)).add(valueToAppend.toString());
}

From source file:Main.java

static void appendProps(Map<String, Object> properties, String key, Iterable<?> valuesToAppend) {
    properties.putIfAbsent(key, new LinkedHashSet<String>());
    StreamSupport.stream(valuesToAppend.spliterator(), false)
            .forEach(v -> ((Collection<String>) properties.get(key)).add(v.toString()));
}

From source file:tds.assessment.services.impl.AssessmentWindowServiceImpl.java

private static <T> Predicate<T> distinctByKey(final Function<? super T, Object> keyExtractor) {
    Map<Object, Boolean> seen = new ConcurrentHashMap<>();
    return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

From source file:org.kuali.rice.core.impl.security.PropertySuppressionServiceImpl.java

private static <T, K, U> Collector<T, ?, Map<K, U>> nullSafeToMap(Function<? super T, ? extends K> keyMapper,
        Function<? super T, ? extends U> valueMapper) {
    return Collectors.collectingAndThen(Collectors.toList(), list -> {
        Map<K, U> result = new HashMap<>();
        for (T item : list) {
            K key = keyMapper.apply(item);
            if (result.putIfAbsent(key, valueMapper.apply(item)) != null) {
                throw new IllegalStateException(String.format("Duplicate key %s", key));
            }/*from   w w  w .j a v  a2  s  . c o  m*/
        }
        return result;
    });
}