Java map

原本以为Java定义的数据结构都是继承于Collection接口,原来map并没有在其中,有单独的Map接口。

java.util.Collection的API中是这样介绍的‘The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements.’顶层接口,包含一组元素。

java.util.Map的API中介绍如下‘An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.’键值对,不包含重复元素,键值一一对应。

定义上好像看不出啥来,就认为两者的数据结构是不同的,接口分离原则就不能允许它们在一起。
Java map

1、HashMap & HashTable

两者都实现了Map接口,继承于不同的抽象类,二者的主要区别是:同步、线程安全性、以及速度。
HashTable是同步的,故线程安全,HashMap不然。所以单线程程序中HashMap更快一些。

2、TreeMap

TreeSet的底层采用TreeMap结构,TreeMap是基于红黑树实现