java 之 雜湊表類 HashTable

來源:互聯網
上載者:User

原文地址:http://www.javaresearch.org/article/showarticle.jsp?column=545&thread=17460

雜湊表是一種重要的儲存方式,也是一種常見的檢索方法。其基本思想是將關係碼的值作為自變數,通過一定的函數關係計算出對應的函數值,把這個數值解釋為結點的儲存地址,將結點存入計算得到儲存地址所對應的儲存單元。檢索時採用檢索關鍵碼的方法。現在雜湊表有一套完整的演算法來進行插入、刪除和解決衝突。在Java中雜湊表用於儲存物件,實現快速檢索。

  Java.util.Hashtable提供了種方法讓使用者使用雜湊表,而不需要考慮其雜湊表真正如何工作。
  雜湊表類中提供了三種構造方法,分別是:
  public Hashtable()
  public Hashtable(int initialcapacity)
  public Hashtable(int initialCapacity,float loadFactor)
  參數initialCapacity是Hashtable的初始容量,它的值應大於0。loadFactor又稱裝載因子,是一個0.0到0.1之間的float型的浮點數。它是一個百分比,表明了雜湊表何時需要擴充,例如,有一雜湊表,容量為100,而裝載因子為0.9,那麼當雜湊表90%的容量已被使用時,此雜湊表會自動擴充成一個更大的雜湊表。如果使用者不賦這些參數,系統會自動進行處理,而不需要使用者操心。
  Hashtable提供了基本的插入、檢索等方法。
  ■插入
  public synchronized void put(Object key,Object value)
給對象value設定一關鍵字key,並將其加到Hashtable中。若此關鍵字已經存在,則將此關鍵字對應的舊對象更新為新的對象Value。這表明在雜湊表中相同的關鍵字不可能對應不同的對象(從雜湊表的基本思想來看,這也是顯而易見的)。
  ■檢索
  public synchronized Object get(Object key)
  根據給定關鍵字key擷取相對應的對象。
  public synchronized boolean containsKey(Object key)
  判斷雜湊表中是否包含關鍵字key。
  public synchronized boolean contains(Object value)
  判斷value是否是雜湊表中的一個元素。
  ■刪除
  public synchronized object remove(object key)
  從雜湊表中刪除關鍵字key所對應的對象。
  public synchronized void clear()
  清除雜湊表
  另外,Hashtalbe還提供方法擷取相對應的枚舉集合:
  public synchronized Enumeration keys()
  返回關鍵字對應的枚舉對象。
  public synchronized Enumeration elements()
  返回元素對應的枚舉對象。
  例8.5 Hashtable.java給出了使用Hashtable的例子。
  例8.5 Hashtalbe.java。
  //import java.lang.*;
  import java.util.Hashtable;
  import java.util.Enumeration;
  public class HashApp{
   public static void main(String args[]){
    Hashtable hash=new Hashtable(2,(float)0.8);
    //建立了一個雜湊表的對象hash,初始容量為2,裝載因子為0.8

    hash.put("Jiangsu","Nanjing");
    //將字串對象“Jiangsu”給定一關鍵字“Nanjing”,並將它加入hash
    hash.put("Beijing","Beijing");
    hash.put("Zhejiang","Hangzhou");

    System.out.println("The hashtable hash1 is: "+hash);
    System.out.println("The size of this hash table is "+hash.size());
    //列印hash的內容和大小

    Enumeration enum1=hash.elements();
    System.out.print("The element of hash is: ");
    while(enum1.hasMoreElements())
     System.out.print(enum1.nextElement()+" ");
    System.out.println();
    //依次列印hash中的內容
    if(hash.containsKey("Jiangsu"))
     System.out.println("The capatial of Jiangsu is "+hash.get("Jiangsu"));
    hash.remove("Beijing");
    //刪除關鍵字Beijing對應對象
    System.out.println("The hashtable hash2 is: "+hash);
    System.out.println("The size of this hash table is "+hash.size());
   }
  }

  運行結果:
  The hashtable hash1 is: {Beijing=Beijing, Zhejiang=Hangzhou, Jiangsu=Nanjing}
  The size of this hash table is 3
  The element of hash is: Beijing Hangzhou Nanjing
  The capatial of Jiangsu is Nanjing
  The hashtable hash2 is: {Zhejiang=Hangzhou, Jiangsu=Nanjing}
  The size of this hash table is 2

  Hashtable是Dictionary(字典)類的子類。在字典類中就把關鍵字對應到資料值。字典類是一個抽象類別。在java.util中還有一個類Properties,它是Hashtable的子類。用它可以進行與對象屬性相關的操作。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.