Hashtableクラス

ハッシュ表を使うとオブジェクトの保存と取得が簡単になります。
ハッシュ表のエントリにはキーと値が含まれます。
キーは一意でなければなりません。値は一意でなくてもよいです。
インディックスを使って要素を取り出すことはできません。
キーを使って値を取り出すことはできます。
このようなデータ構造は連想配列とも呼ばれます。

''Hashtableのコンストラクタ))

#codeprettify{{ hashtable() Hashtable(int n) Hashtable(int n, float lf) }}

引数説明
n初期サイズ
lf荷重率(load factor:有効なデータが全体のサイズにしめる割合)~指定できる値は0~1

エントリ数と荷重率の積を越えた場合はハッシュ表のサイズが自動的に拡張されます。

#codeprettify{{ import java.util.*;

class HashtableDemo{

   public static void main(String args[]){
       //ハッシュ表を作成して情報を設定する
       Hashtable hashtable = new Hashtable();
       hashtable.put("apple","red");
       hashtable.put("strawberry","red");
       hashtable.put("lime","green");
       hashtable.put("banana","yellow");
       hashtable.put("orange","orange");
       //ハッシュ表の要素を表示する
       Enumeration e = hashtable.keys();
       while(e.hasMoreElements()){
           Object k = e.nextElement();         //キーを設定
           Object v = hashtable.get(k);        //キーを使って値を設定
           System.out.println("key = " + k + "; value =" + v);
       }
       //'appleの値を表示する
       System.out.print("\nThe color of apple is: ");
       //キー”アップル”に関連づけられた値を得る
       Object v=hashtable.get("apple");
       System.out.print(v);
   }

} }}

結果

key = strawberry; value =red
key = orange; value =orange
key = apple; value =red
key = lime; value =green
key = banana; value =yellow 

The color of apple is :red

#codeprettify{{ import java.util.*;

class Person {

 String name, telephone, fax, email;
 //Personコンストラクタ
 //ここで情報を記憶する
 Person(String name, String telephone, String fax, String email) {
   this.name = name;
   this.telephone = telephone;
   this.fax = fax;
   this.email = email;
 }
 //情報を文字列で取り出す
 public String toString() {
   return name + "; " + telephone + "; " + fax + "; " + email;
 }

}

class PeopleHashtable {

 public static void main(String args[]) {
   // ハッシュ表を作成し,データを設定する
   Hashtable hashtable = new Hashtable();
   //Person型の変数を用意して、それをhashtableに登録していく
   Person p1 = new Person("Susan", "5634", "2343", "sue");
   hashtable.put("111111111", p1);
   Person p2 = new Person("Claire", "4545", "3331", "claire");
   hashtable.put("222222222", p2);
   Person p3 = new Person("Kim", "9821", "9899", "kim");
   hashtable.put("333333333", p3);
   Person p4 = new Person("Viviane", "4689", "2211", "viv");
   hashtable.put("444444444", p4);
   Person p5 = new Person("Barbara", "1212", "6655", "barb");
   hashtable.put("555555555", p5);
   // ハッシュ表の要素を表示する
   Enumeration e = hashtable.keys();
   while(e.hasMoreElements()) {
     Object k = e.nextElement();
     Object v = hashtable.get(k);
     System.out.println("key = " + k + "; value = " + v);
   }
 }

} }}

結果

c:\>java PeopleHashtable

key = 555555555; value = Barbara; 1212; 6655; barb
key = 333333333; value = kim; 9821; 9899; kim
key = 444444444; value = Viviane; 4689; 2211; viv
key = 111111111; value = Susan; 5634; 2343; sue
key = 222222222; value = Claire; 4545; 3331; claire

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2012-01-11 (水) 00:04:45