From a list Map
with structured key, value
Map<String, String> map = new LinkedHashMap<>();
map.put("param1","valor uno");
map.put("param2","valor dos");
map.put("param3","valor tres");
To convert a list to a string I use the toString
method, to get the data sets separated by lines, I replace some characters of the return of the function toString
public static String mapToMultiLine(Map<String,String> map) {
String str = map.toString().replace("{","").replace("}","").replace(", ","\n");
return str;
}
Now I need to perform the inverse function multiLineToMap
, meaning that from a string with a structure
param1=valor uno
param2=valor dos
param3=valor tres
Converts to a key value list (taking into account that if there is a blank line, delete it.
Update 1 , I have more or less something:
public static Map<String,String> multiLineToMap(String str ) {
str = str.replaceAll("(?m)^[ \t]*\r?\n", ""); //eliminar lineas en blanco
String lines[] = str.split("\n");
//recorrer array lines
// separar key value, delimitador =
}