Difference between HashMap, LinkedHashMap and TreeMap

后端 未结 17 2115
醉话见心
醉话见心 2020-11-22 01:01

What is the difference between HashMap, LinkedHashMap and TreeMap in Java? I don\'t see any difference in the output as all the three

相关标签:
17条回答
  • 2020-11-22 01:32

    HashMap
    can contain one null key.

    HashMap maintains no order.

    TreeMap

    TreeMap can not contain any null key.

    TreeMap maintains ascending order.

    LinkedHashMap

    LinkedHashMap can be used to maintain insertion order, on which keys are inserted into Map or it can also be used to maintain an access order, on which keys are accessed.

    Examples::

    1) HashMap map = new HashMap();

        map.put(null, "Kamran");
        map.put(2, "Ali");
        map.put(5, "From");
        map.put(4, "Dir");`enter code here`
        map.put(3, "Lower");
        for (Map.Entry m : map.entrySet()) {
            System.out.println(m.getKey() + "  " + m.getValue());
        } 
    

    2) TreeMap map = new TreeMap();

        map.put(1, "Kamran");
        map.put(2, "Ali");
        map.put(5, "From");
        map.put(4, "Dir");
        map.put(3, "Lower");
        for (Map.Entry m : map.entrySet()) {
            System.out.println(m.getKey() + "  " + m.getValue());
        }
    

    3) LinkedHashMap map = new LinkedHashMap();

        map.put(1, "Kamran");
        map.put(2, "Ali");
        map.put(5, "From");
        map.put(4, "Dir");
        map.put(3, "Lower");
        for (Map.Entry m : map.entrySet()) {
            System.out.println(m.getKey() + "  " + m.getValue());
        }
    
    0 讨论(0)
  • 2020-11-22 01:33

    HashMap

    • It has pair values(keys,values)
    • NO duplication key values
    • unordered unsorted
    • it allows one null key and more than one null values

    HashTable

    • same as hash map
    • it does not allows null keys and null values

    LinkedHashMap

    • It is ordered version of map implementation
    • Based on linked list and hashing data structures

    TreeMap

    • Ordered and sortered version
    • based on hashing data structures
    0 讨论(0)
  • 2020-11-22 01:33

    All offer a key->value map and a way to iterate through the keys. The most important distinction between these classes are the time guarantees and the ordering of the keys.

    1. HashMap offers 0(1) lookup and insertion. If you iterate through the keys, though, the ordering of the keys is essentially arbitrary. It is implemented by an array of linked lists.
    2. TreeMap offers O(log N) lookup and insertion. Keys are ordered, so if you need to iterate through the keys in sorted order, you can. This means that keys must implement the Comparable interface.TreeMap is implemented by a Red-Black Tree.
    3. LinkedHashMap offers 0(1) lookup and insertion. Keys are ordered by their insertion order. It is implemented by doubly-linked buckets.

    Imagine you passed an empty TreeMap, HashMap, and LinkedHashMap into the following function:

    void insertAndPrint(AbstractMap<Integer, String> map) {
      int[] array= {1, -1, 0};
      for (int x : array) {
        map.put(x, Integer.toString(x));
      }
      for (int k: map.keySet()) {
       System.out.print(k + ", ");
      }
    }
    

    The output for each will look like the results below.

    For HashMap, the output was, in my own tests, { 0, 1, -1}, but it could be any ordering. There is no guarantee on the ordering.
    Treemap,the output was,{ -1, 0, 1}
    LinkedList,the output was,{ 1, -1, 0}

    0 讨论(0)
  • 2020-11-22 01:34

    While there are plenty of excellent Answers here, I'd like to present my own table describing the various Map implementations bundled with Java 11.

    We can see these differences listed on the table graphic:

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

    0 讨论(0)
  • 2020-11-22 01:35

    Just some more input from my own experience with maps, on when I would use each one:

    • HashMap - Most useful when looking for a best-performance (fast) implementation.
    • TreeMap (SortedMap interface) - Most useful when I'm concerned with being able to sort or iterate over the keys in a particular order that I define.
    • LinkedHashMap - Combines advantages of guaranteed ordering from TreeMap without the increased cost of maintaining the TreeMap. (It is almost as fast as the HashMap). In particular, the LinkedHashMap also provides a great starting point for creating a Cache object by overriding the removeEldestEntry() method. This lets you create a Cache object that can expire data using some criteria that you define.
    0 讨论(0)
  • 2020-11-22 01:36

    All three classes HashMap, TreeMap and LinkedHashMap implements java.util.Map interface, and represents mapping from unique key to values.

    HashMap

    1. A HashMap contains values based on the key.

    2. It contains only unique elements.

    3. It may have one null key and multiple null values.

    4. It maintains no order.

      public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable

    LinkedHashMap

    1. A LinkedHashMap contains values based on the key.
    2. It contains only unique elements.
    3. It may have one null key and multiple null values.
    4. It is same as HashMap instead maintains insertion order. //See class deceleration below

      public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>

    TreeMap

    1. A TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
    2. It contains only unique elements.
    3. It cannot have null key but can have multiple null values.
    4. It is same as HashMap instead maintains ascending order(Sorted using the natural order of its key.).

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

    Hashtable

    1. A Hashtable is an array of list. Each list is known as a bucket. The position of bucket is identified by calling the hashcode() method. A Hashtable contains values based on the key.
    2. It contains only unique elements.
    3. It may have not have any null key or value.
    4. It is synchronized.
    5. It is a legacy class.

      public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable

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

    0 讨论(0)
提交回复
热议问题