HashMap, LinkedHashMap和TreeMap在Java中的区别是什么? 我在输出中没有看到任何不同,因为这三个都有keySet和values。什么是哈希表?

Map m1 = new HashMap();
m1.put("map", "HashMap");
m1.put("schildt", "java2");
m1.put("mathew", "Hyden");
m1.put("schildt", "java2s");
print(m1.keySet()); 
print(m1.values()); 

SortedMap sm = new TreeMap();
sm.put("map", "TreeMap");
sm.put("schildt", "java2");
sm.put("mathew", "Hyden");
sm.put("schildt", "java2s");
print(sm.keySet()); 
print(sm.values());

LinkedHashMap lm = new LinkedHashMap();
lm.put("map", "LinkedHashMap");
lm.put("schildt", "java2");
lm.put("mathew", "Hyden");
lm.put("schildt", "java2s");
print(lm.keySet()); 
print(lm.values());

当前回答

虽然这里有很多很好的答案,但我想给出我自己的表,描述与Java 11绑定的各种Map实现。

我们可以在图表中看到这些差异:

HashMap is the general-purpose Map commonly used when you have no special needs. LinkedHashMap extends HashMap, adding this behavior: Maintains an order, the order in which the entries were originally added. Altering the value for key-value entry does not alter its place in the order. TreeMap too maintains an order, but uses either (a) the “natural” order, meaning the value of the compareTo method on the key objects defined on the Comparable interface, or (b) invokes a Comparator implementation you provide. TreeMap implements both the SortedMap interface, and its successor, the NavigableMap interface. NULLs: TreeMap does not allow a NULL as the key, while HashMap & LinkedHashMap do. All three allow NULL as the value. HashTable is legacy, from Java 1. Supplanted by the ConcurrentHashMap class. Quoting the Javadoc: ConcurrentHashMap obeys the same functional specification as Hashtable, and includes versions of methods corresponding to each method of Hashtable.

其他回答

这三个类都实现了Map接口,并提供了基本相同的功能。最重要的区别是迭代条目的顺序:

HashMap绝对不保证迭代顺序。当添加新元素时,它甚至可以(也将会)完全改变。 TreeMap将根据键的compareTo()方法(或外部提供的Comparator)的“自然顺序”进行迭代。此外,它实现了SortedMap接口,该接口包含依赖于此排序顺序的方法。 LinkedHashMap将按照条目放入映射的顺序进行迭代

“哈希表”是基于哈希的映射的通用名称。在Java API的上下文中, Hashtable是Java 1.1时代的一个过时的类,在集合框架存在之前。它不应该再使用了,因为它的API中充满了重复功能的过时方法,而且它的方法是同步的(这会降低性能,而且通常是无用的)。使用ConcurrentHashMap而不是Hashtable。

HashMap

它有一对值(键,值) 禁止复制键值 无序的无序 它允许一个空键和多个空值

哈希表

和哈希映射一样 它不允许空键和空值

LinkedHashMap

它是map实现的有序版本 基于链表和哈希数据结构

TreeMap

有序和排序版本 基于哈希数据结构

HashMap: 不维持秩序 比LinkedHashMap快 用于存储对象堆 LinkedHashMap: LinkedHashMap插入顺序将被维持 比HashMap慢,比TreeMap快 如果你想保持一个插入顺序,使用这个。 TreeMap: TreeMap是一种基于树的映射 TreeMap将遵循键的自然顺序 比HashMap和LinkedHashMap慢 在需要维护自然(默认)排序时使用TreeMap

哈希映射不保留插入顺序。 的例子。Hashmap 如果您正在插入键作为

1  3
5  9
4   6
7   15
3   10

它可以存储为

4  6
5  9
3  10
1  3
7  15

链接Hashmap保留插入顺序。

的例子。 如果您正在插入键

1  3
5  9
4   6
7   15
3   10

它将存储为

1  3
5  9
4   6
7   15
3   10

和我们插入的一样。

树映射以键的递增顺序存储山谷。 的例子。 如果您正在插入键

1  3
5  9
4   6
7   15
3   10

它将存储为

1  3
3  10
4   6
5   9
7   15

以下是HashMap和TreeMap之间的主要区别

HashMap does not maintain any order. In other words , HashMap does not provide any guarantee that the element inserted first will be printed first, where as Just like TreeSet , TreeMap elements are also sorted according to the natural ordering of its elements Internal HashMap implementation use Hashing and TreeMap internally uses Red-Black tree implementation. HashMap can store one null key and many null values.TreeMap can not contain null keys but may contain many null values. HashMap take constant time performance for the basic operations like get and put i.e O(1).According to Oracle docs , TreeMap provides guaranteed log(n) time cost for the get and put method. HashMap is much faster than TreeMap, as performance time of HashMap is constant against the log time TreeMap for most operations. HashMap uses equals() method in comparison while TreeMap uses compareTo() method for maintaining ordering. HashMap implements Map interface while TreeMap implements NavigableMap interface.