1
Map<String,Object> inputMap=new HashMap<String,Object>();

    inputMap.put("a","abc");
    inputMap.put("b","bcd");
    inputMap.put("c","cde");

 HashMap<String,String> subMap=new HashMap<String,String>();
   subMap.put("x","xyz");
    subMap.put("z","klm")

    inputMap.put("d",subMap);




String output=inputMap.toString();
System.out.println(output);


{a=abc,b=bcd,c=cde,d={x=xyz,z=klm}}

the output is : {a=abc,b=bcd,c=cde,d={x=xyz,z=klm}};

How can I convert output back to Map ?

Thanks in advance.

Satyam
  • 13
  • 1
  • 4
  • 1
    Possible duplicate of [Convert string representing key-value pairs to Map](https://stackoverflow.com/questions/14768171/convert-string-representing-key-value-pairs-to-map) – Vince Aug 01 '17 at 02:55
  • Try to use clear() method. It Removes all key/value pairs from the invoking map. – Tehmina Aug 01 '17 at 03:09
  • Here is one solution: https://stackoverflow.com/a/17278809/1270000 – sorifiend Aug 01 '17 at 05:21

2 Answers2

1

Is there a way I can convert the string back to map?

In general, No.

  1. The Map<K,V>.toString() implementations rely on the K.toString() and V.toString() implementations. These are not reversible, which means that the Map implementation cannot be either.

  2. Even in the Map<String,String> case, the rendered map doesn't use any form of escaping to deal with edge-cases ... like keys or values that contain comma, equals or curly brackets.

In restricted cases1, you could implement your own parser, or you could use one of the approaches described here:

However, it is a BAD IDEA to use toString() if you also need the transformation to be reversible. The toString() method contract, and its implementation, are not designed for that purpose. They are primarily designed for use in debug.


1 - For example, if you can assume that K and V are String, there are no null keys or values, and neither will contain awkward characters.

2 - Beware: simple solutions using split etcetera often don't cope with nested maps.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
-1

There is no predefined Java method But You can code with your own logic. Here I am sharing my work example. You can change your code as required

  String output="{a=abc,b=bcd,c=cde,d={x=xyz,z=klm}}";
    if(output.indexOf("{")==0 && output.lastIndexOf("}")==output.length()-1) {
            output=output.substring(1, output.length()-1);
        HashMap<String, String> convertedMap=convert(output);
        for (HashMap.Entry<String, String> value:convertedMap.entrySet()) {
            if(value.getValue().indexOf("{")==0 && value.getValue().lastIndexOf("}")==value.getValue().length()-1) {
               String valueWithBracket=value.getValue();
               convertedMap.remove(value);
               convertedMap=convert(valueWithBracket);
            }
        }
    }

  public static HashMap<String, String> convert(String str) {
    HashMap<String, String> map = new HashMap<String, String>();
        String[] tokens = str.split(",(?![^{)]*\\})");
    for (String token: tokens) {
       String[] val=token.split("=(?![^{)]*\\})");
        for (int i=0;i<val.length;i=i+2) {
            map.put(val[i], val[i+1]);
        }
    }
        return map;
}
public static HashMap<String, String> removeBracketAndConvert(String str){
    if(str.indexOf("{")==0 && str.lastIndexOf("}")==str.length()-1) {
        str=str.substring(1, str.length()-1);
        return convert(str);
    }
    return convert(str);
}
naib khan
  • 928
  • 9
  • 16