Java Utililty Methods Map Replace

List of utility methods to do Map Replace

Description

The list of methods to do Map Replace are organized into topic(s).

Method

StringreplaceCharacters(String sequence, Map map, boolean reverse)
replace Characters
if (reverse) {
    for (Entry<String, String> entry : map.entrySet()) {
        sequence = sequence.replace(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
} else {
    for (Entry<String, String> entry : map.entrySet()) {
        sequence = sequence.replace(String.valueOf(entry.getValue()), String.valueOf(entry.getKey()));
return sequence;
char[]replaceCharactersInArray(char[] characters, Map replacements)
Replaces each instance of any character in the specified map with its corresponding replacement character in a new array of characters that is otherwise equivalent to the specified array of characters.
int charactersLength = characters.length;
char[] replaced = new char[charactersLength];
for (int index = 0; index < charactersLength; index++) {
    char character = characters[index];
    Character replacement = replacements.get(character);
    if (replacement == null) {
        replaced[index] = character;
    } else {
...
voidreplaceElements(Map destinationMap, Map sourceMap)
replace Elements
if (destinationMap != null && sourceMap != null) {
    destinationMap.clear();
    destinationMap.putAll(sourceMap);
StringreplaceEntities(final Map replacements, final String xml)
This function takes the Map generated by the calculateEntityReplacements function, and uses those values to replace any entities in the XML string with their unique random integer replacements.
String retValue = xml;
for (final String entity : replacements.keySet())
    retValue = retValue.replaceAll("\\&" + entity + ";", replacements.get(entity));
return retValue;
StringreplaceFromMap(final String string, final Map replacements)
replace From Map
StringBuilder sb = new StringBuilder(string);
for (Map.Entry<String, String> entry : replacements.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
    final int start = sb.indexOf(key, 0);
    replaceString(sb, key, value, start);
return sb.toString();
...
StringreplaceFromMap(String string, Map replacements)
Replaces all occurrences of keys of the given map in the given string with the associated value in that map.
if (replacements == null) {
    return string;
StringBuilder sb = new StringBuilder(string);
for (Entry<String, String> entry : replacements.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
    int start = sb.indexOf(key, 0);
...
StringreplaceGlobalTokensFromMap(Map dataMap, String message)
Replace global tokens with their map correspondents.
String result = message;
int beginDelim = 0;
while ((beginDelim = result.indexOf("%gbl.")) >= 0) {
    int endDelim = result.indexOf("%", beginDelim + 1);
    String varName = result.substring(beginDelim + 1, endDelim);
    if (dataMap.containsKey(varName)) {
        result = result.replaceAll(result.substring(beginDelim, endDelim + 1),
                (String) dataMap.get(varName));
...
StringreplaceInvalid(String uri, Map reps)
replace Invalid
StringBuilder builder = new StringBuilder();
for (char curChar : uri.toCharArray()) {
    String charStr = String.valueOf(curChar);
    if (reps.containsKey(charStr)) {
        builder.append("%" + reps.get(charStr));
    } else {
        builder.append(charStr);
return builder.toString();
voidreplaceKey(final Map map, final TKey oldkey, final TKey newkey)
replace Key
if (map != null && map.containsKey(oldkey)) {
    map.put(newkey, map.get(oldkey));
} else {
    throw new IllegalArgumentException("Map must be effective and contain the old key.");
voidreplaceKey(Map map, K oldKey, K newKey)
Remove the entry with 'oldKey' and put it (if exists) using 'newKey' unless if an entry already exist for 'newKey'.
if (map == null || map.isEmpty()) {
    return;
if (map.containsKey(newKey)) {
    return;
V o = map.remove(oldKey);
if (o != null) {
...