Why does this cast work?

```
import java.util.HashMap;
import java.util.Map;

public class TestMap {
    public static void main(String[] args) {
        Map<String, Map<String, Map<String, Map<String,Integer>>>> resultMap = new HashMap<>();
        Map<String, Object> aMap = new HashMap<String, Object>();
        Map<String, Integer> hiddenMap = new HashMap<String, Integer>();
        hiddenMap.put("fortytwo", 42);
        aMap.put("key", hiddenMap);
        resultMap =  (Map<String, Map<String, Map<String, Map<String, Integer>>>>) aMap.get("key");
        System.out.println(resultMap);
    }
}

```

also this:

```
Map<String, Map<String, Map<String, Map<String,Map<String,Integer>>>>> resultMap = new HashMap<>();
...
resultMap =  (Map<String, Map<String, Map<String, Map<String,Map<String,Integer>>>>>) aMap.get("key");

```

and so on...

How does this happen that the hidden map which is `Map<String, Integer>` gets successfully cast to `Map<String, Map<String, Map<String, Map<String,Integer>>>> resultMap`?

Always prints:

`{fortytwo=42}`

Also this works (Map instead of Map):

```
public static void main(String[] args) {

        Map<String, Map<String, Map<String, Map<String,Map<String,Integer>>>>> resultMap = new HashMap<>();
        Map<String, Map> aMap = new HashMap<String, Map>();
        Map<String, Integer> hiddenMap = new HashMap<String, Integer>();
        hiddenMap.put("fortytwo", 42);
        aMap.put("key", hiddenMap);
        resultMap =  (Map<String, Map<String, Map<String, Map<String,Map<String,Integer>>>>>) aMap.get("key");
        System.out.println(resultMap);

    }

```

EDIT: So as @shizhz says, it is because of Type Erasure of course! So the code above is equivalent to:

```
Map resultMap = new HashMap();
Map aMap = new HashMap();
Map hiddenMap = new HashMap();
hiddenMap.put("fortytwo", 42);
aMap.put("key", hiddenMap);
resultMap = (Map) aMap.get("key");

```

Which also works