HashMap和MAP
看源码时,用的都是Map<String.Object>这样的形式,而非HashMap<String,Object> hashmap这样的方法。为什么呢?
看看集合的结构图,Map是HashMap、HashTable、TreeMap的父类。

直接声明HashMap也可以用。要考虑到使用Map作为返回值或者方法参数时,那么无论使用者使用HashMap、TreeMap、LinkedHashMap,都可以直接使用它们来接收返回值或者直接传参。而不用更改返回值或参数类型。
那么作为习惯,我们都习惯使用Map<String, Object> map = new HashMap<>();这样的声明方法。

浙公网安备 33010602011771号