Difference between HashMap, LinkedHashMap and TreeMap
2016-08-19 15:29
471 查看
All three classes implement the
and offer mostly the same functionality. The most important difference is the order in which iteration through the entries will happen:
absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added.
iterate according to the "natural ordering" of the keys according to their
(or an externally supplied
Additionally, it implements the
which contains methods that depend on this sort order.
iterate in the order in which the entries were put into the map
"Hashtable" is the generic name for hash-based maps. In the context of the Java API,
an obsolete class from the days of Java 1.1 before the collections framework existed. It should not be used anymore, because its API is cluttered with obsolete methods that duplicate functionality, and its methods are synchronized (which can decrease performance
and is generally useless). Use ConcurrrentHashMap instead
of Hashtable.
Mapinterface
and offer mostly the same functionality. The most important difference is the order in which iteration through the entries will happen:
HashMapmakes
absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added.
TreeMapwill
iterate according to the "natural ordering" of the keys according to their
compareTo()method
(or an externally supplied
Comparator).
Additionally, it implements the
SortedMapinterface,
which contains methods that depend on this sort order.
LinkedHashMapwill
iterate in the order in which the entries were put into the map
"Hashtable" is the generic name for hash-based maps. In the context of the Java API,
Hashtableis
an obsolete class from the days of Java 1.1 before the collections framework existed. It should not be used anymore, because its API is cluttered with obsolete methods that duplicate functionality, and its methods are synchronized (which can decrease performance
and is generally useless). Use ConcurrrentHashMap instead
of Hashtable.
╔══════════════╦═════════════════════╦═══════════════════╦══════════════════════╗ ║ 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 ║ ╚══════════════╩════════════════════════════════════════════════════════════════╝
package com.hashmap; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; public class HashmapTest { public static void main(String[] args) { Map<String,String> map=new HashMap<>(); map.put("a", "aa"); Iterator<Entry<String,String>> iterator=map.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, String> entry = iterator.next(); System.out.println(entry.getKey()); System.out.println(entry.getValue()); } Map<String,String> treeMap=new TreeMap<>(); treeMap.put("b", "bb"); Iterator<Entry<String,String>> iterator2=treeMap.entrySet().iterator(); while (iterator2.hasNext()) { Entry<String,String> entry = iterator2.next(); System.out.println(entry.getKey()); System.out.println(entry.getValue()); } Map<String,String> linkedHashMap=new LinkedHashMap<>(); linkedHashMap.put("c", "cc"); Iterator<Entry<String,String>> iterator3=linkedHashMap.entrySet().iterator(); while (iterator3.hasNext()) { Entry<String,String> entry = iterator3.next(); System.out.println(entry.getKey()); System.out.println(entry.getValue()); } } }
相关文章推荐
- Difference between HashMap, LinkedHashMap and TreeMap in Java
- Differences between HashMap and Hashtable
- Java: Difference between ArrayList and LinkedList
- 6 Difference Between HashMap And HashTable
- Difference between HashMap and HashTable? Can we make hashmap synchronized?
- Java: Difference between ArrayList and LinkedList
- 【Java 源码技术】--Difference between HashMap and ConcurrentHashMap(165)
- Difference between HashMap and Hashtable | HashMap Vs Hashtable
- Differences between direct linked server query and using “OpenQuery”
- Difference between ArrayList and LinkedList in Java
- Difference between HashMap and Hashtable in Java
- 004:Differences between HashMap and Hashtable?
- What is difference between HashMap and Hashtable in Java?
- Difference between HashSet and HashMap in Java
- Difference between ConcurrentHashMap and Collections.synchronizedMap and Hashtable in Java
- Java - Difference between LinkedList and ArrayList
- HashMap与HashTable区别(Differences between HashMap and Hashtable)
- Difference Between HashMap and IdentityHashMap--转
- Differences between hashmap and hashtable
- Java - Difference between HashMap and HashSet in Java