Difference between HashMap, LinkedHashMap and TreeMap

来源:互联网 发布:网络舆情监测平台 编辑:程序博客网 时间:2024/05/16 08:55

All three classes implement the Map interface and offer mostly the same functionality. The most important difference is the order in which iteration through the entries will happen:

  • HashMap makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added.
  • TreeMap will iterate according to the "natural ordering" of the keys according to their compareTo() method (or an externally supplied Comparator). Additionally, it implements the SortedMap interface, which contains methods that depend on this sort order.
  • LinkedHashMap will 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());               }                     }}


0 0