Java Map Replace replaceKey(Map map, K oldKey, K newKey)

Here you can find the source of replaceKey(Map map, K oldKey, K newKey)

Description

Remove the entry with 'oldKey' and put it (if exists) using 'newKey' unless if an entry already exist for 'newKey'.

License

Apache License

Declaration

public static <K, V> void replaceKey(Map<K, V> map, K oldKey, K newKey) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Map;

public class Main {
    /**/*from  ww w  .  j  av  a  2  s  .com*/
     * Remove the entry with 'oldKey' and put it (if exists) using 'newKey' unless if an entry already exist for 'newKey'.
     */
    public static <K, V> void replaceKey(Map<K, V> map, K oldKey, K newKey) {
        if (map == null || map.isEmpty()) {
            return;
        }
        if (map.containsKey(newKey)) {
            return;
        }
        V o = map.remove(oldKey);
        if (o != null) {
            map.put(newKey, o);
        }
    }
}

Related

  1. replaceFromMap(final String string, final Map replacements)
  2. replaceFromMap(String string, Map replacements)
  3. replaceGlobalTokensFromMap(Map dataMap, String message)
  4. replaceInvalid(String uri, Map reps)
  5. replaceKey(final Map map, final TKey oldkey, final TKey newkey)
  6. replaceKeyCharacter(Map map, char oldChar, char newChar)
  7. replaceKeyword(String messageTemplate, Map args, String key)
  8. replaceMap(String inStr, Map replaceMap)
  9. replaceMethodUriWithValues(String methodUri, Map urlParameterKeyValues)