Difference between HashMap, LinkedHashMap and TreeMap in Java
2014-04-09 16:41
176 查看
All the three represent mapping from unique keys to values, and therefore implement the Map interface.
1) HashMap is a map based on hashing of the keys. It makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added. It supports O(1) get/put operations. Keys must have consistent implementations
of hashCode() and equals() for this to work.
2) LinkedHashMap is very similar to HashMap, but it adds awareness to the order at which items are added (or accessed), so the iteration order is the same as insertion order (or access order, depending on construction parameters).
3) TreeMap is a tree based mapping. Its put/get operations take O(log n) time. It requires items to have some comparison mechanism, either with Comparable or Comparator. The iteration order is determined by this mechanism.
1) HashMap is a map based on hashing of the keys. It makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added. It supports O(1) get/put operations. Keys must have consistent implementations
of hashCode() and equals() for this to work.
2) LinkedHashMap is very similar to HashMap, but it adds awareness to the order at which items are added (or accessed), so the iteration order is the same as insertion order (or access order, depending on construction parameters).
3) TreeMap is a tree based mapping. Its put/get operations take O(log n) time. It requires items to have some comparison mechanism, either with Comparable or Comparator. The iteration order is determined by this mechanism.
╔══════════════╦═════════════════════╦═══════════════════╦══════════════════════╗ ║ Property ║ HashMap ║ TreeMap ║ LinkedHashMap ║ ╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣ ║ ║ no guarantee order ║ sorted according ║ ║ ║ Order ║ will remain constant║ to the natural ║ insertion-order ║ ║ ║ over time ║ ordering ║ ║ ╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣ ║ Get/put ║ ║ ║ ║ ║ remove ║ O(1) ║ O(log(n)) ║ O(1) ║ ║ containsKey ║ ║ ║ ║ ╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣ ║ ║ ║ NavigableMap ║ ║ ║ Interfaces ║ Map ║ Map ║ Map ║ ║ ║ ║ SortedMap ║ ║ ╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣ ║ ║ ║ ║ ║ ║ Null ║ allowed ║ only values ║ allowed ║ ║ values/keys ║ ║ ║ ║ ╠══════════════╬═════════════════════╩═══════════════════╩══════════════════════╣ ║ ║ Fail-fast behavior of an iterator cannot be guaranteed ║ ║ Fail-fast ║ impossible to make any hard guarantees in the presence of ║ ║ behavior ║ unsynchronized concurrent modification ║ ╠══════════════╬═════════════════════╦═══════════════════╦══════════════════════╣ ║ ║ ║ ║ ║ ║Implementation║ buckets ║ Red-Black Tree ║ double-linked ║ ║ ║ ║ ║ buckets ║ ╠══════════════╬═════════════════════╩═══════════════════╩══════════════════════╣ ║ Is ║ ║ ║ synchronized ║ implementation is not synchronized ║ ╚══════════════╩════════════════════════════════════════════════════════════════╝
相关文章推荐
- Difference between HashMap and Hashtable in Java
- Difference between ArrayList and LinkedList in Java
- Java - Difference between HashMap and HashSet in Java
- Difference between TreeMap and TreeSet in Java
- Difference between ConcurrentHashMap and Collections.synchronizedMap and Hashtable in Java
- Difference between HashSet and HashMap in Java
- What is difference between HashMap and Hashtable in Java?
- Difference between HashMap, LinkedHashMap and TreeMap
- Difference between throw and throws in Exception handling - Java Example
- Difference between List and Set in Java Collection
- Difference between Externalizable and Serializable in Java
- Difference between Stack and Heap in Java
- Difference Between Class.ForName() And ClassLoader.LoadClass() Methods In Java
- Difference between Set, List and Map in Java - Interview question
- Difference between Vector and ArrayList in java?
- Difference between Stack and Heap memory in Java
- Difference between LinkedList vs ArrayList in Java
- 26.Difference between protocol in objective c and interfaces in java?
- Difference between start and run method in Thread – Java Tutorial
- Difference between <? super T> and <? extends T> in Java