Krishna
Krishna

Reputation: 1

Object to Map conversion

I have a Map of Maps like below in a configuration file

<Map>
      <entry key="C3239">
        <value>
          <Map>
            <entry key="enddate" value="08/12/2015"/>
            <entry key="reason" value="test"/>
          </Map>
        </value>
      </entry>
      <entry key="CAD0139">
        <value>
          <Map>
            <entry key="enddate" value="08/12/2015"/>
            <entry key="reason" value="test 12345"/>
          </Map>
        </value>
      </entry>
    </Map>

i am checking whether entry key(Id =CAD0139) is exist or not .

Config.containsKey(Id);

if the key exists i can get the key, value pair like this Object obj = Config.get(Id); // this returns a object with below key value pair

so now my question is how can i convert an object to Map and retrieve the values of enddate and reason.

Can someone please suggest some easy way to achieve this.

Thanks.

Upvotes: 0

Views: 105

Answers (1)

Bobby StJacques
Bobby StJacques

Reputation: 734

If I understand your question, the simplest solution is to cast the result into a map:

Map value = (Map)Config.get(Id)

I'd recommend making the Map strongly typed, if possible, e.g.

Map<String,Map<String,String>> Config = new HashMap<>();
// then later
if( Config.containsKey(Id)) {
  Map<String,String> value = Config.get(Id);
}

Upvotes: 1

Related Questions