interface Map :implements 有 HashMap,TreeMap;
Map的主要方法:
put();
putAll();
get(key);
remove(key);
containsKey(key);
containsValue(value);
ketSet();
values()(Collection);
entrySet()(key=value…);
isEmpty();
HashMap:
[/color
变量:
// table 里存放 该 Map中全部数据;
[color=darkred]transient Entry[] table;
/////////////////////////////////////////////////////////////////////
Entry<K,V>定义: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).
*/
transient int modCount;
put 方法源代码:
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;
}
void addEntry(int hash, K key, V value, int bucketIndex) { Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}
---------------------------------------------------------------------
---------------------------------------------------------------------
由此可见:
HashMap特点:
1.数据存放在Entry 数组中(table)
2.按照自定义key的hash值来存储。
3.初始化一个 数组,数组的 类型是 Entry的。
4.同一hash 的不同key,存放在 table数组同一 下表处。。。组成一个小链表。
5.put时 如果存在同一key值,则只修改对应的value值。
6.只能存在一个null key。
LinkedHashMap
类定义:
---------------------------------------------------------------------
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;
---------------------------------------------------------------------
由此可见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无序的问题。 但是,其 空间利用率 及效率大大降低。。。
---------------------------------------------------------------------
TreeMap
主要变量:
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
log(n) time cost for the containsKey, get, put and remove operations.
分享到:
相关推荐
Android开发Google_map学习资料 里面有API_GoogleMap中文手册.chm Android定位API使用手册.doc Google_地图_API_专业版开发人员指南.doc Google地图API教程.pdf 还另外发布了 Android SQLite--小巧好用的SQLite GUI...
P231~236C++map学习笔记.docx
Android开发Google_map学习资料 里面有API_GoogleMap中文手册.chm Android定位API使用手册.doc Google_地图_API_专业版开发人员指南.doc Google地图API教程.pdf 还另外发布了 Android SQLite--小巧好用的SQLite GUI...
GooleMAP API例程 可以帮学习了解GoogleMap v3的API
scala学习
linux-kernel-map模块图高清linux-kernel-map模块图高清
geomap4.0-仅供学习使用,下载后24小时请删除,学生课程实习专用。课程实习用课程实习用课程实习用
OneMap Platform v3.0产品白皮书 ,希望对学习OneMap 有帮助
北达科他大学( North Dakota State University)cloud_to_map学习 源文件github地址我找不到了
GeoMap4.0安装包GeoMap4.0安装包GeoMap4.0安装包GeoMap4.0安装包
详尽的介绍了google map api 并简单的举了几个例子,能帮助你了解Google map 编程
Android googlemap 学习交流
本教程适用于Android Map学习的初学者,这是一本很平易近人的Android入门书籍,也是开发者及非开发者两相宜的实务书籍,它能陪伴你顺利入门,并驰骋于无限宽广的Android系统和应用领域
mapobject的中文学习指南,比较经典的教程
Arcmap 学习资料.doc
微信小程序源码 排队取号,map组件使用(学习版)微信小程序源码 排队取号,map组件使用(学习版)微信小程序源码 排队取号,map组件使用(学习版)微信小程序源码 排队取号,map组件使用(学习版)微信小程序源码 排队取号...
学习STL MAP, STL SET之数据结构基础
对于GIS学习的人很有用,可以供你们学习使用!
学习如何基于已有类型定义新的类型:本课涵盖了结构体、数组、slice 和 map。 方法和接口 学习如何为类型定义方法;如何定义接口;以及如何将所有内容贯通起来。 方法和接口 本课包含了方法和接口,可以用它们来定义...
S3C6410手册学习----memory map S3C6410手册学习----memory map S3C6410手册学习----memory map