1.

What is the difference between the HashMap, TreeMap and LinkedHashMap?

Answer»
Characteristic
HashMap
TreeMap
LINKEDHASHMAP
Time complexity to SEARCH and insert
O(1)
O(log n)
O(1)
Iteration Order
Random
Sorted order according to the natural ordering
Sorted order according to the insertion order
Is Null key support
Allowed
Not allowed
Allowed
Is thread safe
No, we need to collections utility to make thread safe
No, we need to collections utility to make thread safe
No, we need to collections utility to make thread safe
Interface
Map
Map, SortedMap, NavigableMap
Map
Data structure support
LinkedList

DoublyLinkedList
Application
Normal processing, faster retrieval ConcurrentHashMap we can USE as per the need
Required when we do the sorting and navigable features need as per the use case.
Required when we need to maintain the insertion order and when we need to implement the LRU cache.
Key requirement
equals() and HASHCODE() method need to overwritten
Along with equals() and hashcode(), the comparator to implement.
equals() and hashcode() method need to overwritten
Syntax
public class HashMap extends AbstractMap
implements Map, Cloneable, Serializable
public class TreeMap extends AbstractMap implements
NavigableMap, Cloneable, Serializable
public class LinkedHashMap extends HashMap
implements Map



Discussion

No Comment Found