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

java中hashtable怎样存储数据和读取数据,第1张

Hashtable-哈希表类\x0d\\x0d\以哈希表的形式存储数据,数据的形式是键值对\x0d\特点:\x0d\查找速度快,遍历相对慢\x0d\键值不能有空指针和重复数据\x0d\\x0d\创建\x0d\Hashtable ht=new \x0d\Hashtable();\x0d\\x0d\添值\x0d\\x0d\htput(1,"Andy");\x0d\htput(2,"Bill");\x0d\htput(3,"Cindy");\x0d\htput(4,"Dell");\x0d\htput(5,"Felex");\x0d\htput(6,"Edinburg");\x0d\htput(7,"Green");\x0d\\x0d\取值\x0d\\x0d\String str=htget(1);\x0d\Systemoutprintln(str);// Andy\x0d\\x0d\对键进行遍历\x0d\\x0d\Iterator it = htkeySet()iterator();\x0d\\x0d\while (ithasNext()) {\x0d\ Integer key = (Integer)itnext();\x0d\ \x0d\Systemoutprintln(key);\x0d\}\x0d\\x0d\对值进行遍历\x0d\\x0d\Iterator it = htvalues()iterator();\x0d\\x0d\while (ithasNext()) {\x0d\ String value =(String) itnext();\x0d\ \x0d\Systemoutprintln(value);\x0d\}\x0d\\x0d\取Hashtable记录数\x0d\\x0d\Hashtable ht=new Hashtable();\x0d\\x0d\htput(1,"Andy");\x0d\htput(2,"Bill");\x0d\htput(3,"Cindy");\x0d\htput(4,"Dell");\x0d\htput(5,"Felex");\x0d\htput(6,"Edinburg");\x0d\htput(7,"Green");\x0d\\x0d\int i=htsize();// 7\x0d\\x0d\删除元素\x0d\\x0d\Hashtable ht=new Hashtable();\x0d\\x0d\htput(1,"Andy");\x0d\htput(2,"Bill");\x0d\htput(3,"Cindy");\x0d\htput(4,"Dell");\x0d\htput(5,"Felex");\x0d\htput(6,"Edinburg");\x0d\htput(7,"Green");\x0d\\x0d\htremove(1);\x0d\htremove(2);\x0d\htremove(3);\x0d\htremove(4);\x0d\\x0d\Systemoutprintln(htsize());// 3\x0d\\x0d\Iterator it = htvalues()iterator();\x0d\\x0d\while (ithasNext()) {\x0d\ // Get value\x0d\ String value =(String) \x0d\itnext();\x0d\ Systemoutprintln(value);\x0d\}

如果实时性要求不是很高可以在timer中这样
hashtable cloneht=_HashTable1copy()(还是_HashTable1clone()的忘了)
然后foreach (string key in clonehtKeys){}

FileReader fr=new FileReader("文本TXT");
FileWriter fw=new FileWriter("文本TXT");
LineNumberReader lr = new LineNumberReader(fr);

try
{
String str = lrreadLine();
int lines = 0;
while (str != null) {
lines++;
str = lrreadLine();
Hashtable ht = new Hashtable();
StringBuilder sb=new StringBuilder();
if(htcontainsKey(str))
{
sbappend(httoString() + " " + str + "\r\n");
}
else
{
htput(lines, str);
}
lines++;
}
fwwrite("sb");


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13174869.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-06-16
下一篇 2023-06-16

发表评论

登录后才能评论

评论列表(0条)

保存