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 -不保存条目的顺序。 如。

public static void main(String[] args){
        HashMap<String,Integer> hashMap = new HashMap<>();
        hashMap.put("First",1);// First ---> 1 is put first in the map
        hashMap.put("Second",2);//Second ---> 2 is put second in the map
        hashMap.put("Third",3); // Third--->3 is put third in the map
        for(Map.Entry<String,Integer> entry : hashMap.entrySet())
        {
            System.out.println(entry.getKey()+"--->"+entry.getValue());
        }
    }

LinkedHashMap:它保存条目的顺序。例如:

public static void main(String[] args){
        LinkedHashMap<String,Integer> linkedHashMap = new LinkedHashMap<>();
        linkedHashMap.put("First",1);// First ---> 1 is put first in the map
        linkedHashMap.put("Second",2);//Second ---> 2 is put second in the map
        linkedHashMap.put("Third",3); // Third--->3 is put third in the map
        for(Map.Entry<String,Integer> entry : linkedHashMap.entrySet())
        {
            System.out.println(entry.getKey()+"--->"+entry.getValue());
        }
    }

TreeMap:按键的升序保存条目。例如:

public static void main(String[] args) throws IOException {
        TreeMap<String,Integer> treeMap = new TreeMap<>();
        treeMap.put("A",1);// A---> 1 is put first in the map
        treeMap.put("C",2);//C---> 2 is put second in the map
        treeMap.put("B",3); //B--->3 is put third in the map
        for(Map.Entry<String,Integer> entry : treeMap.entrySet())
        {
            System.out.println(entry.getKey()+"--->"+entry.getValue());
        }
    }

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

请看下图(大图)中每个类在类层次结构中的位置。TreeMap实现了SortedMap和NavigableMap,而HashMap没有。

HashTable已经过时,应该使用相应的ConcurrentHashMap类。

虽然这里有很多很好的答案,但我想给出我自己的表,描述与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.

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

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