1.

Hashmap Vs Linkedhashmap Vs Treemap In Java?

Answer»
  • HashMap MAKES no guarantees as to the order of the map.

    LinkedHashMap maintains the insertion order of the elements which means if we iterate a LinkedHashMap we'll get the keys in the order in which they were inserted in the Map.

    TreeMap stores objects in sorted order.

  • HashMap as well as LinkedHashMap allows one null as key, multiple values may be null though.

    TreeMap does not allow null as key.

  • HashMap stores elements in a bucket which actually is an index of the array.

    LinkedHashMap also uses the same internal implementation, it also maintains a doubly-linked list running through all of its entries.

    TreeMap is a Red-Black tree based NavigableMap implementation.

  • Performance WISE HashMap provides constant TIME performance O(1) for get() and put() method.

    LinkedHashMap also provides constant time performance O(1) for get() and put() method but in general a little slower than the HashMap as it has to maintain a doubly linked list.

    TreeMap provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.



Discussion

No Comment Found