Skip to content

Commit 0fe2b75

Browse files
committed
优化“HashMap和Hashtable的区别”一文
1 parent 3971cf1 commit 0fe2b75

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ stackoverflow-Java-top-qa
2424
* [如何分割(split)string字符串](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-split-a-string-in-java.md)
2525
* [在java中如何对比(compare)string](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-do-i-compare-strings-in-java.md)
2626
* [`Map<Key,Value>`基于Value值排序](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-sort-a-mapkey-value-on-the-values-in-java.md)
27+
* [`HashMap和Hashtable的区别](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/differences-between-hashmap-and-hashtable.md)
2728

2829
> 编程技巧
2930
@@ -44,7 +45,6 @@ stackoverflow-Java-top-qa
4445
- [Why is processing a sorted array faster than an unsorted array?](http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array)
4546
- [Why is subtracting these two times (in 1927) giving a strange result?](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result)
4647
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
47-
- [Differences between HashMap and Hashtable?](http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable)
4848
- [Creating a memory leak with Java [closed]](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java)
4949
- [Why is char[] preferred over String for passwords?](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords)
5050
- [Generating random integers in a range with Java](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java)

contents/differences-between-hashmap-and-hashtable.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
## HashMap和Hashtable的区别
22
### 问题
33
在Java中`HashMap``Hashtable`的区别?
4-
哪一个对于非线程应用程序更有效
4+
哪一个对于多线程应用程序更好
55

66
### 回答
7-
1. `Hashtable`是同步的,而`HashMap`不是。这让`HashMap`对于非线程应用程序来说更加好一点,对于非同步对象通常表现得比同步的要好。
8-
2. `Hashtable`不允许有空的键或值。`HashMap`允许空的键和任何数值的空值。
9-
3. HashMap的一个子类是[LinkedHashMap](http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html),如果你想预知迭代的顺序(插入的时候就自动排序了),你能轻易的从`LinkedHashMap`转化成`HashMap`。对于`Hashtable`就不那么容易。
10-
如果同步对于你不是那么重要的话,我会建议用`HashMap`.如果同步很重要的话,你可以看看[ConcurrentHashMap](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html)
7+
1. `Hashtable`是同步的,加了`synchronized`锁,而`HashMap`不是。没有加`synchronized`锁的对象,性能通常比加了`synchronized`锁的对象要更好一些,因此,如果是非多线程程序,不需要考虑锁、同步等问题,那么使用`HashMap`更好。
8+
2. `Hashtable`不允许有空的键或值。`HashMap`允许空键和空值。
9+
3. HashMap有一个子类[LinkedHashMap](http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html),对这个类对象进行迭代时,它的顺序是有序的(按插入顺序排序)。如有需要,你也能轻易的从`LinkedHashMap`转化成`HashMap``Hashtable`就没那么简单了,
10+
11+
总之,如果你无需关心同步(synchronized)问题,我会建议用`HashMap`。反之,你可以参考[ConcurrentHashMap](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html)
1112

1213
### stackoverflow链接:
1314
http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable

0 commit comments

Comments
 (0)