问题

之间有什么区别 HashMap Hashtable ?

这对于非线程应用程序更有效?



解决方法

HashMap和Hashtable在Java中有几个区别:

  1. Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.

  2. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.

  3. 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 the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.

由于同步不是您的问题,我建议您使用 HashMap .如果同步出现问题,您还可以查看 ConcurrentHashMap .




相关问题推荐