集合类-Map

来源:互联网 发布:淘宝好吃的泡面排行榜 编辑:程序博客网 时间:2024/06/14 06:54

Map存储键值对形势的数据,保存的元素总是成对出现的。键不能重复,值可以重复。

继承关系
Map没有父接口,不是继承自Collection。
Map下有HashMap、HashTable、WeakHashMap、IdentityHashMap、TreeMap实现类,HashMap下有LinkedHashMap子类。
HashMap
HashMap基于散列表实现,继承了AbstractMap抽象类,AbstractMap中已经实现了部分Map的功能方法。实现了Map、Cloneable、Serializable接口。

public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable

LinkedHashMap
LinkedHashMap继承自HashMap类,功能类似于HashMap,但是遍历时取出来的键值是插入顺序。因为有链表维护内部元素的顺序,迭代速度比HashMap快。

public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>

HashTable
操作元素相关的方法使用了synchronized关键字,实现了同步的Map。

public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable

WeakHashMap
弱键映射,允许释放映射所指的对象。

public class WeakHashMap<K,V> extends AbstractMap<K,V> implements Map<K,V> {

IdentityHashMap
用==代替equals对键值进行比较的特殊Map实现。

public class IdentityHashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Serializable, Cloneable

TreeMap
基于红黑树实现,里面的键值会被排序。具有subMap返回一个子集的方法。

public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, Serializable
原创粉丝点击