# Question
What are the differences between a HashMap
and a Hashtable
in Java?
Which is more efficient for non-threaded applications?
# Answer
There are several differences between HashMap
and Hashtable
in Java:
Hashtable
is synchronized, whereasHashMap
is not. This makesHashMap
better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.Hashtable
does not allownull
keys or values.HashMap
allows onenull
key and any number ofnull
values.One of HashMap's subclasses is
LinkedHashMap
, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out theHashMap
for aLinkedHashMap
. This wouldn't be as easy if you were usingHashtable
.
Since synchronization is not an issue for you, I'd recommend HashMap
. If synchronization becomes an issue, you may also look at ConcurrentHashMap
.
source: https://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable
'Language > java' 카테고리의 다른 글
우분투에서 자바(Java) 설치하는 방법 (쉬워요) (0) | 2018.12.22 |
---|---|
How do I generate random integers within a specific range in Java? (0) | 2018.06.28 |
Create ArrayList from array (0) | 2018.06.28 |
Read/convert an InputStream to a String (0) | 2018.06.28 |