0

Say I have a Java map:

[
"Level 1.1":"Value1.1",
"Level 1.2":[
    "Level2.1":"Value2.1"
    "Level2.2":[
     "Level3.1":"Value3.1"
    ]
 ]
]

Keys at all level are unique. Order of the keys can be changed at each level. It could have more then three levels.

I want to create a method called removeKey(). What it does is it will take an argument, or a list of arguments, and iterate through this map and remove that key at all levels.

So for example, calling removeKey("Level3.1") will remove that key on the third level down, and the map left will be:

[
"Level 1.1":"Value1.1",
"Level 1.2":[
    "Level2.1":"Value2.1"
    "Level2.2":[
    ]
 ]
]

Calling removeKey("Level3.1", "Level 2.2") will result in:

[
"Level 1.1":"Value1.1",
"Level 1.2":[
    "Level2.1":"Value2.1"
 ]
]

Can you please help me with the method definition?

Thanks heaps.

nzsquall
  • 395
  • 1
  • 10
  • 27
  • 2
    Yes, if you show us what you have got so far ... – Henry Jan 13 '15 at 06:44
  • Can you show us the declaration of the map? The value is sometimes a String and sometimes a Map. Have you declared it as `Map`? – sprinter Jan 13 '15 at 06:44
  • `Keys at all level are mostly unique` - keys in a HashMap must be unique – Eran Jan 13 '15 at 06:44
  • 1
    You've tagged this with "hashmap" which makes me concerned that you also talk about the *order* of the keys, as plain hash maps are unordered. It's also not clear what the type of the map is, given that some of your values are maps, and some are plain values. Can you show some Java code setting this up? – Jon Skeet Jan 13 '15 at 06:45
  • @nzsquall: Are you stuck/confused as how the value in the HashMap would be? If you have the declared the Map then my hint to you would be recursion. – Keen Sage Jan 13 '15 at 06:47
  • Sorry for the confusion, the keys are unique at all levels. I define this map in a Groovy class, and trying to manipulate it in another Java class. So this map is return from a Groovy class method as a LinkedHashMap, and I declare it as a generic Java Map type. – nzsquall Jan 13 '15 at 06:58
  • @Sashwat Yes I am a bit stuck and confused. – nzsquall Jan 13 '15 at 06:59
  • @Henry Sorry I don't have much. I think the reference I found here at http://stackoverflow.com/questions/6092642/how-to-remove-a-key-from-hashmap-while-iterating-over-it could be useful, but not applicable here. – nzsquall Jan 13 '15 at 07:00
  • Your compiler will be complaining about raw types. But for any nested data structure like this you should use recursion. – Raedwald Jan 13 '15 at 08:12

1 Answers1

0

Below is a recursive function that will remove keys from all levels of the map.

public static void removeKey(Map<String, Object> levels, String ... keys) {
    if (levels == null
            || (levels != null && levels.size() == 0)) {
        return;
    }

    for (String k : keys) {
        levels.remove(k);
    }

    for (String key : levels.keySet()) {
        if (levels.get(key) instanceof Map) {
            removeKey((Map<String, Object>) levels.get(key), keys);
        } 
    }
}
asohun
  • 331
  • 3
  • 7
  • 3
    Nice solution. `if (levels == null || levels.size() == 0)` should be enough... Also, iterating over `levels.entrySet()` will save you a couple of `.get` calls in the body :) – xpa1492 Jan 13 '15 at 07:26