服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java高级:集合框架中的map接口的使用

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接口的样子:

  1. public interface java.util.map {
  2.       //altering methods
  3.       public object put(object key, object value);    //gets object with key/value mapping
  4.       public object remove(object key);               //removes object with key
  5.       public void putall(java.util.map);              //put all map elements into current map
  6.       public void clear();                            //removes all mappings from current map
  7.       //querying methods
  8.       public object get(object key);                  //gets object with key
  9.       public int size();                              //returns number of map elements
  10.       public boolean isempty();                       //check if map is empty
  11.       public boolean containskey(object);             //checks if map contains object as key
  12.       public boolean containsvalue(object);           //checks if map contains object as value
  13.       public boolean equals(object);                  //compares specified object with current map
  14.       //viewing methods
  15.       public java.util.set keyset();                  //gets keys
  16.       public java.util.collection values();           //gets values
  17.       public java.util.set entryset();                //gets mappings
  18.       public static interface java.util.map.entry {   //a map-entry (single key/value pair)
  19.            public object getkey();                    //returns current entry key
  20.            public object getvalue();                  //returns current entry value
  21.            public object setvalue(object value);      //replaces current value with specified value
  22.            public boolean equals(object);             //compares current object with specified object
  23.            public int hashcode();                     //returns hashcode with current map-entry
  24.       }
  25. }

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接口中没有的)有:

  1. public class java.util.hashtable extends dictionary implements cloneablemapserializable {
  2.      //hashtable constructors
  3.           //construct a default hashtable with default capacity and load of 0.75
  4.      public hashtable();                     
  5.           //construct a hashtable with passed capacity and default load of 0.75 
  6.      public hashtable (int initialcapacity); 
  7.           //construct hashtable with passed capacity and load 
  8.      public hashtable(int initialcapacity, float load); 
  9.      public hashtable(map);                  //construct hashtable with passed mapping
  10.      
  11.      //hashtable specific methods
  12.      public boolean contains(object);        //checks if object is in hashtable 
  13.      public enumeration elements();          //returns enumeration of elements in hashtable
  14.      public enumeration keys();              //returns enumeration of keys in hashtable
  15.           //creates shallow copy of hashtable(structure copied, but not key/values)
  16.      public object clone();                  
  17.      public string tostring();               //prints out key/value pairs of hashtable elements
  18.           //reorganizes all elements in hashtable, and increases hashtable capacity
  19.      protected void rehash();                
  20.      
  21.      public object get(object);              //get value from passed in key
  22.      public object put(object key, object value);      //insert key/value pair
  23. }

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 

扫描关注微信公众号