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());

当前回答

@Amit: SortedMap is an interface whereas TreeMap is a class which implements the SortedMap interface. That means if follows the protocol which SortedMap asks its implementers to do. A tree unless implemented as search tree, can't give you ordered data because tree can be any kind of tree. So to make TreeMap work like Sorted order, it implements SortedMap ( e.g, Binary Search Tree - BST, balanced BST like AVL and R-B Tree , even Ternary Search Tree - mostly used for iterative searches in ordered way ).

public class TreeMap<K,V>
extends AbstractMap<K,V>
implements SortedMap<K,V>, Cloneable, Serializable

在坚果壳 HashMap:给出O(1)的数据,没有排序

TreeMap:给出O(log N),以2为底的数据。使用有序键

LinkedHashMap:是具有链表(想想索引- skiplist)功能的哈希表,以插入树的方式存储数据。最适合实现LRU(最近最少使用)。

其他回答

哈希映射不保留插入顺序。 的例子。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

@Amit: SortedMap is an interface whereas TreeMap is a class which implements the SortedMap interface. That means if follows the protocol which SortedMap asks its implementers to do. A tree unless implemented as search tree, can't give you ordered data because tree can be any kind of tree. So to make TreeMap work like Sorted order, it implements SortedMap ( e.g, Binary Search Tree - BST, balanced BST like AVL and R-B Tree , even Ternary Search Tree - mostly used for iterative searches in ordered way ).

public class TreeMap<K,V>
extends AbstractMap<K,V>
implements SortedMap<K,V>, Cloneable, Serializable

在坚果壳 HashMap:给出O(1)的数据,没有排序

TreeMap:给出O(log N),以2为底的数据。使用有序键

LinkedHashMap:是具有链表(想想索引- skiplist)功能的哈希表,以插入树的方式存储数据。最适合实现LRU(最近最少使用)。

HashMap、TreeMap和LinkedHashMap这三个类都实现了java.util.Map接口,并表示从唯一键到值的映射。

HashMap

HashMap包含基于键的值。 它只包含独特的元素。 它可以有一个空键和多个空值。 它没有维持秩序。 公共类HashMap<K,V>扩展了AbstractMap<K,V>实现了Map<K,V>,可克隆,可序列化

LinkedHashMap

LinkedHashMap包含基于键的值。 它只包含独特的元素。 它可以有一个空键和多个空值。 它与HashMap相同,只是维护插入顺序。//见下面的减速等级 公共类LinkedHashMap<K,V>扩展HashMap<K,V>实现Map<K,V>

TreeMap

TreeMap包含基于键的值。它实现了NavigableMap接口并扩展了AbstractMap类。 它只包含独特的元素。 它不能有空键,但可以有多个空值。 它与HashMap相同,只是维护升序(使用键的自然顺序进行排序)。 公共类TreeMap<K,V>扩展AbstractMap<K,V>实现NavigableMap<K,V>,可克隆,可序列化

哈希表

哈希表是一个列表数组。每个列表都被称为一个桶。桶的位置通过调用hashcode()方法来标识。哈希表包含基于键的值。 它只包含独特的元素。 它可能没有任何空键或值。 它是同步的。 这是一个传承类。 公共类Hashtable<K,V>扩展Dictionary<K,V>实现Map<K,V>,可克隆,可序列化

裁判:http://javarevisited.blogspot.in/2015/08/difference-between-HashMap-vs-TreeMap-vs-LinkedHashMap-Java.html

这三个都表示从唯一键到值的映射,因此实现了Map接口。

HashMap is a map based on hashing of the keys. It supports O(1) get/put operations. Keys must have consistent implementations of hashCode() and equals() for this to work. 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). 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.

这些是同一接口的不同实现。每种实现都有一些优点和缺点(快速插入,缓慢搜索),反之亦然。

有关详细信息,请参阅TreeMap, HashMap, LinkedHashMap的javadoc。