首先说说相同点,LinkedHashMap, HashMap和TreeHashMap都implement Map interface,从功能性上来说基本上是相同的。
不同点:
1. HashMap不保证元素的顺序,即添加的HashMap的顺序和储存在HashMap内的顺序是不一致的
2. TreeMap中的元素遵循natural order,使用compareTo()方法使内容按照key有序
3. LinkedHashMap保证元素的结果和添加元素的顺序一致
public class SeveralMaps {
public static void main(String[] args) {
Map hashMap = new HashMap();
hashMap.put("6", "6");
hashMap.put("1", "1");
hashMap.put("2", "2");
hashMap.put("5", "5");
hashMap.put("3", "3");
hashMap.put("4", "4");
System.out.println("*********** HashMap *************");
System.out.print(hashMap.keySet());
System.out.println(hashMap.values());
Map treeMap = new TreeMap();
treeMap.put("6", "6");
treeMap.put("1", "1");
treeMap.put("2", "2");
treeMap.put("5", "5");
treeMap.put("3", "3");
treeMap.put("4", "4");
System.out.println("*********** TreeMap *************");
System.out.print(treeMap.keySet());
System.out.println(treeMap.values());
Map linkedHashMap = new LinkedHashMap();
linkedHashMap.put("6", "6");
linkedHashMap.put("1", "1");
linkedHashMap.put("2", "2");
linkedHashMap.put("5", "5");
linkedHashMap.put("3", "3");
linkedHashMap.put("4", "4");
System.out.println("*********** LinkedHashMap *************");
System.out.print(linkedHashMap.keySet());
System.out.println(linkedHashMap.values());
}
}
代码的运行结果为
*********** HashMap *************
[1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6]
*********** TreeMap *************
[1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6]
*********** LinkedHashMap *************
[6, 1, 2, 5, 3, 4][6, 1, 2, 5, 3, 4]
╔══════════════╦═════════════════════╦═══════════════════╦══════════════════════╗
║ Property ║ HashMap ║ TreeMap ║ LinkedHashMap ║
╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣
║ ║ no guarantee order ║ sorted according ║ ║
║ Order ║ will remain constant║ to the natural ║ insertion-order ║
║ ║ over time ║ ordering ║ ║
╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣
║ Get/put ║ ║ ║ ║
║ remove ║ Θ (1) ║ Θ (log(n)) ║ Θ (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 ║
╚══════════════╩════════════════════════════════════════════════════════════════╝
考虑多线程操作的话使用ConcurrentHashMap会有更高的效率
文章代码链接https://github.com/AlanJager/java_learning/blob/master/src/SeveralMaps.java