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

Stringreplace(String template, Map funcAndDic)
replace
if (funcAndDic == null) {
    return template;
for (Entry<String, String> entry : funcAndDic.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
    String[] items = key.split("-");
    String replaceStr = items[1].replaceAll("\\$", "\\\\\\$").replaceAll("\\{", "\\\\\\{")
...
Stringreplace(String text, Map pairs)
replace
StringBuilder result = new StringBuilder(text);
for (String old : pairs.keySet()) {
    if (old != null) {
        if (!old.equals("")) {
            replaceString(result, old, pairs.get(old));
return result.toString();
Stringreplace2(String str, Map data)
Replace 2.
for (String key : data.keySet()) {
    str = str.replaceAll(key, data.get(key));
return str;
StringreplaceAll(Map globals, String value)
Substitute all global variables of the form $(KEY) in a string.
String retval = value;
if (value != null && globals != null) {
    for (String key : globals.keySet()) {
        String var = "${" + key + "}";
        if (value.contains(var) && globals.get(key) != null) {
            retval = retval.replace(var, globals.get(key));
return retval;
StringreplaceAll(String inTemplate, Map inVars)
replace All
if (inTemplate == null || inVars == null) {
    return inTemplate;
for (final Entry<String, String> e : inVars.entrySet()) {
    if (e.getKey() == null) {
        continue;
    inTemplate = inTemplate.replace(e.getKey(), e.getValue() == null ? e.getKey() : e.getValue());
...
StringreplaceAll(String template, Map variables)
Given a raw input string template, this will do a mass search and replace for the map of variables to values.
if (template == null || variables == null || variables.isEmpty()) {
    return template;
for (Map.Entry<String, String> entry : variables.entrySet()) {
    String value = entry.getValue();
    if (value == null) {
        value = EMPTY;
    } else {
...
StringreplaceAllOccurrences(String str, Map replacementMap)
replace All Occurrences
Set<Map.Entry<String, String>> entries = replacementMap.entrySet();
Iterator<Map.Entry<String, String>> it = entries.iterator();
while (it.hasNext()) {
    Map.Entry<String, String> entry = it.next();
    str = replaceOccurrences(str, entry.getKey(), entry.getValue());
return str;
StringreplaceAllRegex(String input, Map replacements)
replace All Regex
if (input == null) {
    return null;
Iterator<Entry<String, String>> replacementsI = replacements.entrySet().iterator();
Entry<String, String> replacement;
String current = input;
while (replacementsI.hasNext()) {
    replacement = replacementsI.next();
...
StringreplaceByMap(String _searchStr, Map _replacements)
Replace all placeholders in given string by value of the corresponding key in given Map.
if (_searchStr == null) {
    return null;
if (_replacements == null || _replacements.isEmpty()) {
    return _searchStr;
String str = _searchStr;
for (Entry<String, String> entry : _replacements.entrySet()) {
...
StringreplaceByMap(String tpl, Map map)
replace like xxxx{key}yyy{key}zzzz
if (tpl != null && map != null && map.size() > 0) {
    for (String key : map.keySet()) {
        tpl = tpl.replace("{" + key + "}", String.valueOf(map.get(key)));
return tpl;