Posts Tagged to string
Flatening Java Servlet Request Maps
Some times it is necessary to flatten a map into a string. For example when trying to log a servlet request onto a single log line without loosing too much info. In this case each value is an array of strings. So here it is, short and effective. Enjoy!
private String mapToString(Map map) {
StringBuffer buffer = new StringBuffer();
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
String[] value = (String[]) map.get(key);
buffer.append(key + "=");
for (int c = 0; c < value.length; c++) {
buffer.append("[" + value[c] + "]");
}
if (it.hasNext()) {
buffer.append(":");
}
}
return buffer.toString();
}