Java Collections Framework(3) Map

来源:互联网 发布:手机桌面 知乎 编辑:程序博客网 时间:2024/05/21 17:52
1.A Map is an object that maps keys to values. A map cannot contain duplicate keys, each key can at most map to one value.
2.Map iuterface provides three collection views , which allow a  map's contents  to be viewed as a set of keys , collection of values, or set of key-value mappings.


HashMap:

1)HashMap is implemented as a hash table. 
2)Hashing is designed to solve the problem of needing to find or store an item in a collection. Hashing means using some function or algorithm to map object data to a representative integer value. This so-called hash code can then be used as a way to narrow down our search when looking for an item in the map.
3)散列表(也称哈希表,Hash Table),是根据码值(Key Value)而进行直接访问的数据结构。它通过关键码值映射到表中的一个位置来访问记录,以增加查找速度。若结构中存在关键字和K相等的记录,则必定存储在f(K)的位置上。由此,不需比较便可直接取得所查记录。这个映射函数叫做散列函数,存放记录的数组叫做哈希表。
4)HashMap中的Key都有一个hashCode().  
5)列表的存储方式是基于数组,不同的关键字可能得到同一散列地址,即key1≠key2,而f(key1)=f(key2),这种现象称冲突,为了解决这个冲突,数组的的值都是一个链表,Entry,包含Key、Value以及指向下一个Entry的next.
 

6)put method

public V put(K key, V value) {        if (key == null)            return putForNullKey(value);        int hash = hash(key.hashCode());//目的是让“1”变的均匀一点,散列的本意就是要尽量均匀分布。        int i = indexFor(hash, table.length);//进行逻辑与运算,即 h & (length-1),这样得到的结果就是一个比length小的正数,我们把这个值叫做index。        for (Entry<K,V> e = table[i]; e != null; e = e.next) {            Object k;            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {                V oldValue = e.value;                e.value = value;                e.recordAccess(this);                return oldValue;            }        }        modCount++;        addEntry(hash, key, value, i);//如果index.size>thredhold=default_load_factor*capacity,则需要resize        return null;    }


7)Hashtable通过initial capacity和load factor两个参数调整性能。通常缺省的load factor 0.75较好地实现了时间和空间的均衡。增大load factor可以节省空间但相应的查找时间将增大,这会影响像get和put这样的操作。 
8)HashMap不是同步的,但key允许有null值
9)如果一个对象,覆盖了equals(Object)方法,则也要重写hashCode方法。若e1.equals(e2),则e1.hashCode()e2.hashCode().

假如同一个对象有两个不同的hashCode,那么get时有可能发生Null的情况



TreeMap
1)A Red-Black tree based implementation. The map is sorted according to the Comparable natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
2)A red-black tree is a type of self-balancing binary search tree. The balancing of the tree guarantees searching in O(log(n)) Time, where n is the total number of elements in the tree. The insertion, deletion operations, along with the tree rearrangement and recoloring are also performed in O(log(n)) Time.
3)如果Map创建的时候指定了Comparator比较器,则按照Comparator.compare(Obj1,Obj2)来比较两个Key,否则按照Key自身的实现的Comparable.compareTo(Obj)来比较。
4)假定使用 Comparator c 将满足 (a.equals(b) && c.compare(a, b) != 0) 的两个元素 a 和 b 添加到一个空 TreeSet(TreeMap) 中,则第二个 add(put) 操作将返回 true(TreeMap/TreeSet 的大小将会增加),因为从Map/Set 的角度来看,a 和 b 是不相等的,即使这与 Map/Set的规范相反。


LinkedHashMap
1)LinkedHashMap的Entry元素继承HashMap的Entry,提供了双向链表的功能。继承HashMap的Entry元素,又保存了其上一个元素before和下一个元素after的引用。 
2)LinkedHashMap与HashMap比较,它维护着一个运行于所有条目(table[0]-table[table.length-1])的双重链接列表。此链接列表定义了迭代顺序(accessOrder),该迭代顺序可以是插入顺序或者是访问顺序。accessOrder在创建时需要指定。
3)迭代遍历时会按照accessOrder设定的顺序遍历数据。


HashTable
1)同步的
2)key不允许有null值
3) HashTable的改进,HashTable中的算法只是把key的hashcode与length相除取余,即hash % length,这样有可能会造成index分布不均匀。还有一点需要说明,HashMap的键可以为null,它的值是放在数组的第一个位置。
原创粉丝点击