遍历hashtable 遍历的几种方法

遍历hashMap、hashSet、Hashtable[转]
一.遍历HashMap
Map&Integer, String& map = new
HashMap&Integer, String&();
i=0;i&100;i++)
&&&map.put(i,
方法一:效率比方法二高
for(Entry&Integer, String&
entry:map.entrySet())
&&&System.out.println(entry.getKey()+"="+entry.getValue());
for(Object obj : map.keySet())
Object key =
Object value =
map.get(obj);&&&&&
System.out.println(value);
二.遍历HashSet
Set set = new HashSet();
i=0;i&100;i++)
.add("123");
for(Iterator it=set.iterator();it.hasNext();)
&&&System.out.println(it.next());
三.遍历Hashtable(同步、线程安全的)
Hashtable table = new Hashtable();
&&table.put(1, "1");
&&table.put(2, "1");
&&table.put(3, "1");
&&//遍历key
&&Enumeration e =
table.keys();
&&while( e. hasMoreElements()
&&System.out.println(
e.nextElement() );
&&//遍历value
&&e = table.elements();
&&while( e. hasMoreElements()
&&System.out.println(
e.nextElement() );
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。Java Hashtable多线程操作遍历问题_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Java Hashtable多线程操作遍历问题
来源:Linux社区&
作者:luoyahu
最近发现程序经常报出java.util.ConcurrentModificationException异常.发现其一个互斥作用的hashtable线程周期性去除无用key报错,导致hashtable值不断增大.
检测线程为单独线程,每一小时检测一次,使用java的iterator进行遍历.问题就出在iterator.在使用iterator遍历时不能使用原hashtable的put与remove方法,要不就会报java.util.ConcurrentModificationException异常,上锁的话可能会造成性能问题.
经过测试优化的处理方式为改为
& &Enumeration&String& e1 = T1.map.keys();& &while (e1.hasMoreElements()) { & & String key = e1.nextElement();& & String ret = T1.map.remove(key);& &}
以后还是少用iterator为妙.
以下为测试程序,有兴趣可以跑跑
public class T1 extends Thread{&public static Hashtable&String, String& map = new Hashtable&String, String&();&&&public void run() {& super.run();& while(true)& {& &for (int i = 0; i & 100; i++) {& & map.put(String.valueOf(System.currentTimeMillis())+i, "");& &}& &System.out.println("add 100----&"+map.size());& &Random rd = new Random();& &try {& & Thread.sleep(rd.nextInt(10));& &} catch (InterruptedException e) {& & e.printStackTrace();& &}& }&}&&&public static void main(String[] args) throws InterruptedException {& T1 t1 = new T1();& t1.start();& T2 t2 = new T2();& t2.start();&}}
public class T2 extends Thread{&&&&public void run() {& super.run();& while(true)& {& &System.out.println("map----size 01:"+T1.map.size());//& &异常的逻辑//& &Iterator it = T1.map.entrySet().iterator();//& &while (it.hasNext()) {//& & it.next();//& & it.remove();//& &}& && &Enumeration&String& e1 = T1.map.keys();& &while (e1.hasMoreElements()) { & & String key = e1.nextElement();& & String ret = T1.map.remove(key);& &} & && &System.out.println("map----size 02:"+T1.map.size());& && &Random rd = new Random();& &try {& & Thread.sleep(rd.nextInt(10));& &} catch (InterruptedException e) {& & e.printStackTrace();& &}& }&}&}
如果有其它更好的方法或意见可以联系我.大家一起讨论一下。
相关资讯 & & &
& (04/15/:08)
& (12/29/:43)
& (03/24/:16)
图片资讯 & & &
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款如何在Hashtable中进行遍历?谢谢!
[问题点数:0分]
如何在Hashtable中进行遍历?谢谢!
[问题点数:0分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
相关帖子推荐:
2002年3月 Java大版内专家分月排行榜第二2002年2月 Java大版内专家分月排行榜第二2001年10月 Java大版内专家分月排行榜第二
2002年3月 Java大版内专家分月排行榜第二2002年2月 Java大版内专家分月排行榜第二2001年10月 Java大版内专家分月排行榜第二
本帖子已过去太久远了,不再提供回复功能。         
您现在的位置:&&>&&>&&>&&>&&>&正文
JAVA技巧:HashMap和Hashtable的遍历
来源:()  【】 
  一.HashMap遍历,两种方法  1.keySet()  Person person1=new Person("zhangsan",20);  Person person2=new Person("lisi",21);  Person person3=new Person("wangwu",22);  Map hm=new HashMap();  hm.put("first", person1);  hm.put("second", person2);  hm.put("three", person3);  Set keys=ht.keySet();  Iterator it=keys.iterator();  //遍历取出  while(it.hasNext()){  String key=(String)it.next();  Person person=(Person)ht.get(key);  System.out.println(person.getName()+" "+person.getAge());  }entrySet()  Person person1=new Person("zhangsan",20);  Person person2=new Person("lisi",21);  Person person3=new Person("wangwu",22);  Map hm=new HashMap();  hm.put("first", person1);  hm.put("second", person2);  hm.put("three", person3);  Set keys=hm.entrySet();  Iterator it=keys.iterator();  while(it.hasNext()){  Map.Entry me=(Map.Entry)it.next();  String key=(String)me.getKey();  Person value=(Person)me.getValue();  System.out.println("key : "+key);  System.out.println("value : "+value.getName()+" "+value.getAge());  }  二.Hashtable遍历  Hashtable ht = new Hashtable();//不能Map ht=new Hashtable();若加强制转换后,后面方法不能用  ht.put("first", person1);  ht.put("second", person2);  ht.put("three", person3);  Enumeration e=ht.elements();  while(e.hasMoreElements()){  Person person=(Person)e.nextElement();  System.out.println(person.getName()+" "+person.getAge());  }         1&&&
文章责编:gaoxiaoliang& 看了本文的网友还看了
?&&( 15:51:47)?&&( 14:16:20)?&&( 14:14:40)?&&( 14:13:00)?&&( 14:10:55)?&&( 14:02:35)
? ?   ? ?   ? ?   ? ?   ? ?
? ?   ? ?   ?
?   ? ?    ? ?   ? ?   ? ?   ? ?
? ?   ? ?
实用工具 |
| 大全 | 大全
     |
版权声明:如果网所转载内容不慎侵犯了您的权益,请与我们联系,我们将会及时处理。如转载本内容,请注明出处。
Copyright & 2004-
 网 All Rights Reserved 
中国科学院研究生院权威支持(北京) 电 话:010- 传 真:010-C# Dictionary&&和HashTable中遍历Keys怎么写?_百度知道
C# Dictionary&&和HashTable中遍历Keys怎么写?
比如说建立了一个类Student包含字段name,age,hobby用name作为KeysDictionary&string, Student& stu = new Dictionary&string, Student&();stu.Add(s1.Name,s1);stu.Add(s2.Name,s2);stu.Add(s3.Name,s3);添加好了数据 遍历Value可以写foreach (Student ss in stu.Values)
Student ass = (Student)
MessageBox.Show(ass.Name.ToString());
}那么遍历Keys都时候具体怎么写?foreach (Student ss in stu.Keys)
提问者采纳
foreach (Student ss in stu.Values) { //Student ass = (Student)//这句话没用还是错误的 MessageBox.Show(ss.Name.ToString()); //ass改成ss } 那么遍历Keys都时候具体怎么写? //foreach (Student ss in stu.Keys)//这句话是错误的KEYS是string foreach (string key in stu.Keys) //这样遍历 { MessageBox.Show(key);} }
提问者评价
对对就是这样谢谢
其他类似问题
hashtable的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 中序遍历 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信