1. 导言
随着java[tm] 2标准版中的集合框架的引入,一组通用数据结构接口被整合到了java[tm] 2 sdk,以简化程序员的工作,使程序员可以专注于业务需求,而不是构造数据对象。这个新的框架为用户提供了一些有用的工具和功能,用户不需要对框架的细节知道很多,就可以很好地使用它。在java[tm]集合框架中,有两个主要的接口,(1)collection接口,包含list和set子接口,(2)map接口。collection和map接口之间的主要区别在于:collection中存储了一组对象,而map存储关键字/值对。在map对象中,每一个关键字最多有一个关联的值。一个很好的日常的例子就是把人们的profile信息和他的社会安全号(相当于中国的身份证号)进行关联。社会安全号是关键字,而profile就是对应的人映射到的值。
2. map接口
下面的代码片断显示了map接口的样子:
- public interface java.util.map {
- //altering methods
- public object put(object key, object value); //gets object with key/value mapping
- public object remove(object key); //removes object with key
- public void putall(java.util.map); //put all map elements into current map
- public void clear(); //removes all mappings from current map
- //querying methods
- public object get(object key); //gets object with key
- public int size(); //returns number of map elements
- public boolean isempty(); //check if map is empty
- public boolean containskey(object); //checks if map contains object as key
- public boolean containsvalue(object); //checks if map contains object as value
- public boolean equals(object); //compares specified object with current map
- //viewing methods
- public java.util.set keyset(); //gets keys
- public java.util.collection values(); //gets values
- public java.util.set entryset(); //gets mappings
- public static interface java.util.map.entry { //a map-entry (single key/value pair)
- public object getkey(); //returns current entry key
- public object getvalue(); //returns current entry value
- public object setvalue(object value); //replaces current value with specified value
- public boolean equals(object); //compares current object with specified object
- public int hashcode(); //returns hashcode with current map-entry
- }
- }
map接口为我们提供了完成下面三种主要的功能的方法:
1. map 改变
2. map 查询
3. map 视图
map的改变方法允许用户改变当前map的内容,包括关键字/值对的删除、更新和插入。
map的查询方法允许用户从map中获取关键字/值对。不但有查询map元素的内容的方法,也有可以用来查询整个map对象的方法。
map中一共有三种不同的视图可以用来分析关键字/值对。既然映射中的关键字必须唯一,那么,keyset()方法获取的是map中的关键字的一个set(set是唯一数据元素的集合)。values()方法返回映射中值的collection(collection是允许存储重复元素的对象的集合)。entryset()方法返回map.entrhy的一个set。
map.entry接口是用来存储单个关键字/值对的。在map.entry中有存储和获取单个关键字/值元素的方法。entryset()方法返回一组实现了map.entry接口的对象的set。set中的每一个元素都代表了map中的一个独立的关键字/值对。
3. map 的实现
下面的部分将map的三个通用实现作一个简单介绍:
1. java.util.hashtable
2. java.util.hashmap
3. java.util.treemap
3.1 java.util.hashtable
hashtable对象把关键字对象映射到值对象。提供了允许基于关键字搜索的快速查找的方法。hashtable是在java 1.0平台中引入的,而下面将要讨论的hashmap是在java 1.2平台引入的。hashtable提供的附加方法(map接口中没有的)有:
- public class java.util.hashtable extends dictionary implements cloneable, map, serializable {
- //hashtable constructors
- //construct a default hashtable with default capacity and load of 0.75
- public hashtable();
- //construct a hashtable with passed capacity and default load of 0.75
- public hashtable (int initialcapacity);
- //construct hashtable with passed capacity and load
- public hashtable(int initialcapacity, float load);
- public hashtable(map); //construct hashtable with passed mapping
- //hashtable specific methods
- public boolean contains(object); //checks if object is in hashtable
- public enumeration elements(); //returns enumeration of elements in hashtable
- public enumeration keys(); //returns enumeration of keys in hashtable
- //creates shallow copy of hashtable(structure copied, but not key/values)
- public object clone();
- public string tostring(); //prints out key/value pairs of hashtable elements
- //reorganizes all elements in hashtable, and increases hashtable capacity
- protected void rehash();
- public object get(object); //get value from passed in key
- public object put(object key, object value); //insert key/value pair
- }
hashtable类似于常见的关键字映射到值的表格,但是hashtable提供了提取数据的快速方法。当一个元素插入到hashtable中时,关键字对象被散列(原文:the name of the object is hashed,似有不妥),返回的整数值作为值对象在表中存储的索引值。 然后,值对象存储为散列索引所指向的(表格)单元的值(译注:这儿的说法可以理解,但欠准确。在hashtable的实现中在每个单元中存储的是一个包含了散列码、关键字、值和指向下一个entry的引用的map.entry的实现对象)。如果,另外具有相同散列码的对象也要插入hashtable,则该对象将被存储在一个原条目开始的链表中。
hashtable的初始容量指示了hashtable中需要分配的空间。由于hashtable是一个动态的实体,需要不断地大小缩放来为hashtable高效地分配空间。装载因子指示了在hashtable的容量需要自动增长之前,容许的空间的百分比占用。
the initial capacity of the hashtable dictates how many spaces are allocated for objects in the hashtable. as a hashtable is a dynamic entity, constant resizing is required to efficiently allocate space for the hashtable. the load factor indicates how full percentage wise the hashtable is allowed to become before the hashtable's capacity is automatically increased.
需要注意的两点是(1)hashtable是同步的,(2)hashtable中不允许关键字或值为null。
two things to note are that 
闽公网安备 35060202000074号