HashMap和MAP

看源码时,用的都是Map<String.Object>这样的形式,而非HashMap<String,Object> hashmap这样的方法。为什么呢?

看看集合的结构图,Map是HashMap、HashTable、TreeMap的父类。
image

直接声明HashMap也可以用。要考虑到使用Map作为返回值或者方法参数时,那么无论使用者使用HashMap、TreeMap、LinkedHashMap,都可以直接使用它们来接收返回值或者直接传参。而不用更改返回值或参数类型。

那么作为习惯,我们都习惯使用Map<String, Object> map = new HashMap<>();这样的声明方法。

posted @ 2021-09-26 17:35  快乐的海盗  阅读(420)  评论(0编辑  收藏  举报