Explain the differences between treemap, hashmap, and linkedhashmap. Provide an example of when each one would be best.
Answers
HashMap:
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.
Syntax: public class HashMap extends AbstractMap implements Map,Cloneable, Serializable
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.
LinkedHashMap:
LinkedHashMap offers 0(1)lookup and insertion. Keys are ordered by their insertion order. It is implemented by doubly-linked buckets.
Syntax: public class LinkedHashMap extends HashMap 0implements Map
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.
TreeMap:
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.
Syntax: public class TreeMap extends AbstractMap implements NavigableMap, Cloneable, Serializable
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).
HOPE IT'S HELP YOU
THANKYOU
mermaid.queen.9876