java中hashtable怎样存储数据和读取数据

如题所述

Hashtable-哈希表类\x0d\x0a\x0d\x0a以哈希表的形式存储数据,数据的形式是键值对.\x0d\x0a特点:\x0d\x0a查找速度快,遍历相对慢\x0d\x0a键值不能有空指针和重复数据\x0d\x0a\x0d\x0a创建\x0d\x0aHashtable ht=new \x0d\x0aHashtable();\x0d\x0a\x0d\x0a添值\x0d\x0a\x0d\x0aht.put(1,"Andy");\x0d\x0aht.put(2,"Bill");\x0d\x0aht.put(3,"Cindy");\x0d\x0aht.put(4,"Dell");\x0d\x0aht.put(5,"Felex");\x0d\x0aht.put(6,"Edinburg");\x0d\x0aht.put(7,"Green");\x0d\x0a\x0d\x0a取值\x0d\x0a\x0d\x0aString str=ht.get(1);\x0d\x0aSystem.out.println(str);// Andy\x0d\x0a\x0d\x0a对键进行遍历\x0d\x0a\x0d\x0aIterator it = ht.keySet().iterator();\x0d\x0a\x0d\x0awhile (it.hasNext()) {\x0d\x0a Integer key = (Integer)it.next();\x0d\x0a \x0d\x0aSystem.out.println(key);\x0d\x0a}\x0d\x0a\x0d\x0a对值进行遍历\x0d\x0a\x0d\x0aIterator it = ht.values().iterator();\x0d\x0a\x0d\x0awhile (it.hasNext()) {\x0d\x0a String value =(String) it.next();\x0d\x0a \x0d\x0aSystem.out.println(value);\x0d\x0a}\x0d\x0a\x0d\x0a取Hashtable记录数\x0d\x0a\x0d\x0aHashtable ht=new Hashtable();\x0d\x0a\x0d\x0aht.put(1,"Andy");\x0d\x0aht.put(2,"Bill");\x0d\x0aht.put(3,"Cindy");\x0d\x0aht.put(4,"Dell");\x0d\x0aht.put(5,"Felex");\x0d\x0aht.put(6,"Edinburg");\x0d\x0aht.put(7,"Green");\x0d\x0a\x0d\x0aint i=ht.size();// 7\x0d\x0a\x0d\x0a删除元素\x0d\x0a\x0d\x0aHashtable ht=new Hashtable();\x0d\x0a\x0d\x0aht.put(1,"Andy");\x0d\x0aht.put(2,"Bill");\x0d\x0aht.put(3,"Cindy");\x0d\x0aht.put(4,"Dell");\x0d\x0aht.put(5,"Felex");\x0d\x0aht.put(6,"Edinburg");\x0d\x0aht.put(7,"Green");\x0d\x0a\x0d\x0aht.remove(1);\x0d\x0aht.remove(2);\x0d\x0aht.remove(3);\x0d\x0aht.remove(4);\x0d\x0a\x0d\x0aSystem.out.println(ht.size());// 3\x0d\x0a\x0d\x0aIterator it = ht.values().iterator();\x0d\x0a\x0d\x0awhile (it.hasNext()) {\x0d\x0a // Get value\x0d\x0a String value =(String) \x0d\x0ait.next();\x0d\x0a System.out.println(value);\x0d\x0a}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-03-20
Hashtable-哈希表类

以哈希表的形式存储数据,数据的形式是键值对.
特点:
查找速度快,遍历相对慢
键值不能有空指针和重复数据

创建
Hashtable<Integer,String> ht=new
Hashtable<Integer,String>();

添值

ht.put(1,"Andy");
ht.put(2,"Bill");
ht.put(3,"Cindy");
ht.put(4,"Dell");
ht.put(5,"Felex");
ht.put(6,"Edinburg");
ht.put(7,"Green");

取值

String str=ht.get(1);
System.out.println(str);// Andy

对键进行遍历

Iterator it = ht.keySet().iterator();

while (it.hasNext()) {
Integer key = (Integer)it.next();

System.out.println(key);
}

对值进行遍历

Iterator it = ht.values().iterator();

while (it.hasNext()) {
String value =(String) it.next();

System.out.println(value);
}

取Hashtable记录数

Hashtable<Integer,String> ht=new Hashtable<Integer,String>();

ht.put(1,"Andy");
ht.put(2,"Bill");
ht.put(3,"Cindy");
ht.put(4,"Dell");
ht.put(5,"Felex");
ht.put(6,"Edinburg");
ht.put(7,"Green");

int i=ht.size();// 7

删除元素

Hashtable<Integer,String> ht=new Hashtable<Integer,String>();

ht.put(1,"Andy");
ht.put(2,"Bill");
ht.put(3,"Cindy");
ht.put(4,"Dell");
ht.put(5,"Felex");
ht.put(6,"Edinburg");
ht.put(7,"Green");

ht.remove(1);
ht.remove(2);
ht.remove(3);
ht.remove(4);

System.out.println(ht.size());// 3

Iterator it = ht.values().iterator();

while (it.hasNext()) {
// Get value
String value =(String)
it.next();
System.out.println(value);
}本回答被提问者和网友采纳
相似回答