Question

I am getting a String which comes from calling toString() from a List<Map<String, String>> object. Some sample code is:

List<Map<String,String>> list = new ArrayList<Map<String,String>>();
Map<String, String> map = new HashMap<String, String>();

map.put("key1", "value1");
map.put("key2", "value2");

list.add(map);

String myStr = list.toString();
System.out.println(myStr);

This code outputs:

[{key2=value2, key1=value1}]

I am looking for a simple way to parse this string and convert it to a List<Map<String, String>> again. I am just wondering if there is any constructor / utility class out there to do this easily. I have already look at Java Collections library and Guava from Google but I have not found any function already built to do this. Does this exist?

If not I will just create my own parser but I would to reuse some code if it exists out there. I like the concept of DRY.

Was it helpful?

Solution

In general, you can't do this using the built in toString method because it isn't doing anything to make the values parse-able, it's just trying to give you a representation that's helpful. As a particularly perverse example, consider the following code that demonstrates that two distinct maps can have the same printed representation:

import java.util.Map;
import java.util.TreeMap;

public class MapToStringExample {
    public static void main(String[] args) {
        Map<String,String> map1 = new TreeMap<>();
        map1.put( "key1", "value1, key2=value2" );
        System.out.println( "Map1: "+map1 );

        Map<String,String> map2 = new TreeMap<>();
        map2.put( "key1", "value1" );
        map2.put( "key2", "value2" );
        System.out.println( "Map2: "+map2 );
    }
}
Map1: {key1=value1, key2=value2}
Map2: {key1=value1, key2=value2}

If you want to read in the printed representation of a Map like this, you'll either need to make some restrictions on what values you can put into it (e.g., nothing containing an = or ,), or you'll need to write your own output routine).

If you're looking to avoid rewriting code, you might look into the serialization API that would handler serializing and deserializing values for you. Of course, in this case, where you're just mapping strings to strings, all you need is a way to write alternating keys and values separated by some delimiter, and a way to escape that delimiter in the string representation (if it's allowed to appear in strings; if it's not, then you don't even need to escape it). That's not too hard of a serialization and deserialization task.

OTHER TIPS

This is what i mean in the comment:

Gson gson = new Gson();
List<Map<String,String>> list = Lists.newArrayList();
Map<String, String> map = Maps.newHashMap();

map.put("key1", "value1");
map.put("key2", "value2");

list.add(map);

String myStr = gson.toJson(list);
System.out.println(myStr);

List<Map<String,String>> newList = gson.fromJson(myStr, new TypeToken<List<Map<String, String>>>(){}.getType());

System.out.println(newList);

output:

[{"key2":"value2","key1":"value1"}]
[{key2=value2, key1=value1}]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top