Java Collections: Map

Map Interface

What is a map in java? In java map is an interface that extends the collection interface. A map structures its elements in a unique key value pair by using a hash table data structure. A map is unordered by default. Map can be implemented by TreeMap, HashMap, and LinkedHashMap classes.

HashTable

A HashTable implements the map interface. HashTable allows doesn’t allow null key or values. HashTable is synchronized, which can be useful for threaded applications.

HashMap

A HashMap implements the map interface. HashMap is an implementation of a hash table data structure. It allows one null key in an element. HashMap is non-synchronized, because of this it’s faster than a HashTable.

LinkedHashMap

The LinkedHashMap class extends HashMap. A LinkedHashMap maintains insertion order unlike a HashMap.

TreeMap

TreeMap implements SortedMap interface which extends the map interface. SortedMap maintains the key value pairs in a sorted ascending order by default. The TreeMap uses a tree data structure, because of tree structure its access and retrieval time are fast O(log n) logarithmic time complexity. Also, TreeMap doesn’t take null keys

Leave a comment

Design a site like this with WordPress.com
Get started