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

当前回答

HashMap

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

哈希表

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

LinkedHashMap

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

TreeMap

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

其他回答

这三个都表示从唯一键到值的映射,因此实现了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.

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

三者中最重要的是如何保存条目的顺序。

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和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.

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

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

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