Tania
Tania

Reputation: 1925

Comparing two nested hashmaps

I have two nested hashmaps of the following structure(levels of nesting can vary) The nested values can be instances of maps or arrays of maps.

{

  key1:[

    {

      nkey1:[

        nval1,
        nval2
      ]
    },
    {

      nkey2:[

        nval3,
        nval4
      ]
    }
  ]
}

Currently I am doing an equals operation on two maps, which expects all keys to be identical. I want to exclude certain keys while comparison.

Say I should say I want to exclude key1::nkey2. How Do I accomplish this? A map.keySet() does not help with nested maps. How do I efficiently do a key by key comparison walking over this nested map?

Upvotes: 0

Views: 1219

Answers (1)

bashnesnos
bashnesnos

Reputation: 816

You can flatten your map, i.e. include folded keys into main map. For your example you would have keys:

"key1"
"key1:nkey1"
"key1:nkey2"

In this case you won't need to use deep equals on each key, since all the levels would be eventually checked anyway. And you could remove any keys you want while traversing a single keySet().

Upvotes: 1

Related Questions