Here you can find the source of putIfAbsentGet(Map
Parameter | Description |
---|---|
K | the map key type |
V | the map value type |
map | the map to populate, not null |
key | the key |
value | the value |
public static <K, V> V putIfAbsentGet(Map<K, V> map, K key, V value)
//package com.java2s; /**//from ww w . j a va 2 s . c o m * Copyright (C) 2011 - present by OpenGamma Inc. and the OpenGamma group of companies * * Please see distribution for license. */ import java.util.Map; public class Main { /** * Puts the value into the map if the key is not present. * <p> * This is most useful in building up a map of maps, or similar structure. * * @param <K> the map key type * @param <V> the map value type * @param map the map to populate, not null * @param key the key * @param value the value * @return the contents of the map for the key */ public static <K, V> V putIfAbsentGet(Map<K, V> map, K key, V value) { V existing = map.get(key); if (existing != null) { return existing; } map.put(key, value); return value; } }