zoukankan      html  css  js  c++  java
  • java集合类散列表

    哈希表

         是种数据结构,它可以提供快速的插入操作和查找操作。第一次接触哈希表时,它的优点多得让人难以置信。不论哈希表中有多少数据,

    插入和删除(有时包括侧除)只需要接近常量的时间即0(1)的时间级。实际上,这只需要几条机器指令。

      对哈希表的使用者一一人来说,这是一瞬间的事。哈希表运算得非常快,在计算机程序中,如果需要在一秒种内查找上千条记录通常

    使用哈希表(例如拼写检查器)哈希表的速度明显比树快,树的操作通常需要O(N)的时间级。哈希表不仅速度快,编程实现也相对容易。

      哈希表也有一些缺点它是基于数组的,数组创建后难于扩展某些哈希表被基本填满时,性能下降得非常严重,所以程序虽必须要清楚

    表中将要存储多少数据(或者准备好定期地把数据转移到更大的哈希表中,这是个费时的过程)。

      而且,也没有一种简便的方法可以以任何一种顺序〔例如从小到大〕遍历表中数据项。如果需要这种能力,就只能选择其他数据结构。

    然而如果不需要有序遍历数据,井且可以提前预测数据量的大小。那么哈希表在速度和易用性方面是无与伦比的。

    Hashtable(散列表)

    //该例子中,说明HashtableDemo和Enumeration类的使用
    import java.util.Enumeration;
    import java.util.Hashtable;

    public class HashtableDemo {
       public static void main(String[] args){
          Hashtable<Integer,String>table=new Hashtable<Integer,String>();
          table.put(new Integer("1"), "one");
          table.put(new Integer("2"), "two");
          table.put(new Integer("3"), "three");
          table.put(new Integer("4"), "four");
         System.out.println("用for循环遍历Hashtable");
        for(int i=1;i<=table.size();i++){
           System.out.print((String)table.get(i)+" ");   
        }
         System.out.println(" 用Enumeration遍历Hashtable");
        Enumeration<String>enu =table.elements();
        while(enu.hasMoreElements()){
           System.out.print(enu.nextElement()+" ");
          }
       }
    }

  • 相关阅读:
    Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse. ......
    UVA 1597 Searching the Web
    UVA 1596 Bug Hunt
    UVA 230 Borrowers
    UVA 221 Urban Elevations
    UVA 814 The Letter Carrier's Rounds
    UVA 207 PGA Tour Prize Money
    UVA 1592 Database
    UVA 540 Team Queue
    UVA 12096 The SetStack Computer
  • 原文地址:https://www.cnblogs.com/mibin/p/6617021.html
Copyright © 2011-2022 走看看