1

I use this list:

List<HashMap<Map<String, Object>, Map<String, Object>>>

And I need to save it to a text file.

So with the List#toString method, I obtain a text like that:

[{{key=value, key=value, key=value}={key=value, key=value}}, {{key=value, key=value, key=value}={key=value, key=value}}, {{key=value, key=value, key=value}={key=value, key=value}}]

How can I convert it back to a List?

Thanks!

sphinks
  • 3,048
  • 8
  • 39
  • 55

3 Answers3

2

You are better off using a format like JSON or YAML. Usng toString() means there is too many corner cases like a = { } or , appearing in a key or value.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

The toString() method produces a format that is intended for logging and debugging, not for serialization and deserialization.

I recommend to convert your object into json format (other formats like xml can work just as well) and use a json library such as "Gson" for conversions.

Example how you can convert your object into a json string:

Gson gson = new Gson(); 
String json = gson.toJson(myObject);

Example how you can convert the json string back into an object, see: creating Hashmap from a JSON String

Update: Here's a complete example of the first part:

List<HashMap<Map<String, Object>, Map<String, Object>>> myObject = new ArrayList<>();
HashMap<Map<String, Object>, Map<String, Object>> hashMap1 = new HashMap<>();
myObject.add(hashMap1);

Map<String, Object> key1 = new HashMap<>();
key1.put("key1", "keyValue1");
Map<String, Object> value1 = new HashMap<>();
value1.put("value1", "valueValue1");
hashMap1.put(key1, value1);

Gson gson = new GsonBuilder().disableHtmlEscaping().create();
String json = gson.toJson(myObject);
System.out.println(json);

This example prints:

[{"{key1=keyValue1}":{"value1":"valueValue1"}}]

Update2 Use Pair (from apache commons lang3) instead of HashMap. That makes a lot more sense.

List<Pair<Map<String, Object>, Map<String, Object>>> myObject = new ArrayList<>();
MutablePair<Map<String, Object>, Map<String, Object>> pair1 = new MutablePair<>();
myObject.add(pair1);

Map<String, Object> key1 = new HashMap<>();
key1.put("key1", "keyValue1");
Map<String, Object> value1 = new HashMap<>();
value1.put("value1", "valueValue1");
pair1.setLeft(key1);
pair1.setRight(value1);

Gson gson = new Gson();
String json = gson.toJson(myObject);
System.out.println(json);

Prints:

[{"left":{"key1":"keyValue1"},"right":{"value1":"valueValue1"}}]
Community
  • 1
  • 1
Andreas Vogl
  • 1,776
  • 1
  • 14
  • 18
  • thanks! Nice idea, i will use Json but does i need to convert every map or can i just use Gson#toJson on to List: List, Map>> –  Mar 02 '16 at 10:40
  • You can pass your entire list/map structure into gson.toJson() - that's the easy part. Converting back is slightly more difficult, you can either use fromJson() method or write your own series of nested loops building your object from json dom. – Andreas Vogl Mar 02 '16 at 10:53
  • I've try to use Gson#toJson but in the Json, all elements of the list are empty. –  Mar 02 '16 at 10:57
  • I've added a working example above. While doing that I realized that using a map as key in another hashmap is really an extremely odd thing to do. Gson converts the key-map into a string, because there is no better way to model that in json format. I'd strongly recommend creating a custom java class to model your data structure rather than using nested maps. – Andreas Vogl Mar 02 '16 at 11:36
0
ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map1=new HashMap<>();
        map1.put("key","value");
        map1.put("key1","value");
        map1.put("key2","value");
        map1.put("key3","value");
        HashMap<Map<String, Object>, Map<String, Object>> hash=new HashMap<>();

        List<HashMap<Map<String, Object>, Map<String, Object>>> lists=new ArrayList<>();
        hash.put(map1, map1);
        lists.add(hash);
        System.out.println(mapper.writeValueAsString(lists));