Hashtable和HashMap的区别:

引用
1.Hashtable是Dictionary的子类,HashMap是Map接口的一个实现类;

2.Hashtable中的方法是同步的,而HashMap中的方法在缺省情况下是非同步的。即是说,在多线程应用程序中,不用专门的操作就安全地可以使用Hashtable了;而对于HashMap,则需要额外的同步机制。但HashMap的同步问题可通过Collections的一个静态方法得到解决:
Map Collections.synchronizedMap(Map m)
这个方法返回一个同步的Map,这个Map封装了底层的HashMap的所有方法,使得底层的HashMap即使是在多线程的环境中也是安全的。

3.在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键,而应该用containsKey()方法来判断。


参考遍历的使用:

Iterator<String> iterator = default_config.keySet().iterator();
while(iterator.hasNext()) {
  Object oj = iterator.next();
  default_config.get(oj).toString();
  // key = oj.toString()
  // value = default_config.get(oj).toString()
}


另外,关注了一下android下的一些操作的效率:

引用
一些标准操作的时间比较

为了距离说明我们的观点,下面有一张表,包括一些基本操作所使用的大概时间。注意这些时间并不是绝对的时间,绝对时间要考虑到CPU和时钟频率。系统不同,时间的大小也会有所差别。当然,这也是一种有意义的比较方法,我们可以比叫不同操作花费的相对时间。例如,添加一个成员变量的时间是添加一个本地变量的四倍。


比较数据:

引用
Action
Time

Add a local variable
1

Add a member variable
4

Call String.length()
5

Call empty static native method
5

Call empty static method
12

Call empty virtual method
12.5

Call empty interface method
15

Call Iterator:next() on a HashMap
165

Call put() on a HashMap
600

Inflate 1 View from XML
22,000

Inflate 1 LinearLayout containing 1 TextView
25,000

Inflate 1 LinearLayout containing 6 View objects
100,000

Inflate 1 LinearLayout containing 6 TextView objects
135,000

Launch an empty activity
3,000,000


参考资料:
1 HashMap遍历
2 编写高效的Android代码


原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
Tags: , ,
发表评论
表情
emotemotemotemotemotemotemotemotemotemotemotemotemot
emotemotemotemotemotemotemotemotemotemotemotemot
打开HTML 打开UBB 打开表情 隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 不区分大小写
 

阅读推荐

服务器相关推荐

开发相关推荐

应用软件推荐