Map 学习

interface Map :implements 有 HashMap,TreeMap;
Map的主要方法:
[align=left][size=x-small][color=red]put();
putAll();
get(key);
remove(key);
containsKey(key);
containsValue(value);
ketSet();
values()(Collection);
entrySet()(key=value…);
isEmpty();[/color][/size][/align]

[color=red][size=medium][align=center]HashMap:[/align][/size][/color
变量:

// table 里存放 该 Map中全部数据;
[color=darkred]transient Entry[] table;[/color]
/
[color=darkred]Entry<K,V>定义:[/color]static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
final int hash;

/**
* Creates new entry.
*/
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
/
* The number of times this HashMap has been structurally modified
* Structural modifications are those that change the number of mappings in
* the HashMap or otherwise modify its internal structure (e.g.,
* rehash). This field is used to make iterators on Collection-views of
* the HashMap fail-fast. (See ConcurrentModificationException).
*/
[color=darkred]transient int modCount;[/color]
[color=darkred]put 方法源代码:[/color]

public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
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);
return null;
}

[color=darkred]void addEntry(int hash, K key, V value, int bucketIndex) {[/color] Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}

---------------------------------------------------------------------
---------------------------------------------------------------------
由此可见:
[color=blue]HashMap特点:
1.数据存放在Entry 数组中(table)
2.按照自定义key的hash值来存储。
3.初始化一个 数组,数组的 类型是 Entry的。
4.同一hash 的不同key,存放在 table数组同一 下表处。。。组成一个小链表。
5.put时 如果存在同一key值,则只修改对应的value值。
6.只能存在一个null key。[/color]

[color=red][size=medium][align=center]LinkedHashMap[/align][/size][/color]

类定义:
---------------------------------------------------------------------
public class LinkedHashMap<K,V>
extends HashMap<K,V>
implements Map<K,V>
{ private transient Entry<K,V> header;.....
}
---------------------------------------------------------------------
private static class Entry<K,V> extends HashMap.Entry<K,V> {
// These fields comprise the doubly linked list used for iteration.
Entry<K,V> before, after;
---------------------------------------------------------------------
[color=blue]由此可见LinkedHashMap所有的成员变量如下:

transient Entry[] table;
private transient Entry<K,V> header;

Entry<K,V> 变量如下:
final K key;
V value;
Entry<K,V> next;
final int hash;
Entry<K,V> before, after;
所以:LinkedHashMap用 双向链表即实现了Map又实现了链表。。解决了HashMap无序的问题。 但是,其 空间利用率 及效率大大降低。。。[/color]
---------------------------------------------------------------------

[color=red][size=medium][align=center]TreeMap[/align][/size][/color]
主要变量:
private final Comparator<? super K> comparator;
private transient Entry<K,V> root = null;

static final class Entry<K,V> implements Map.Entry<K,V> {
K key;
V value;
Entry<K,V> left = null;
Entry<K,V> right = null;
Entry<K,V> parent;
boolean color = BLACK;
排序是按照 Comparator来判断,如果该变量为null,自然排序。否则,按照Comparator来排序...
This implementation provides guaranteed [size=x-small][color=red]log(n) [/color][/size]time cost for the containsKey, get, put and remove operations.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值