Last Updated : 23 Jul, 2025
Prerequisite: HashMap and TreeMap in Java
TreeMap, HashMap and LinkedHashMap: What's Similar?
Key Points:
public class HashMap extends AbstractMap
implements Map,Cloneable, Serializable
2. 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
implements Map
3.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
4. Hashtable: "Hashtable" is the generic name for hash-based maps.
Syntax:
public class Hashtable extends Dictionary implements
Map, Cloneable, Serializable
// Java program to print ordering
// of all elements using HashMap
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
// This function prints ordering of all elements
static void insertAndPrint(AbstractMap<Integer, String> map)
{
int[] array= {1, -1, 0, 2,-2};
for (int x: array)
{
map.put(x, Integer.toString(x));
}
for (int k: map.keySet())
{
System.out.print(k + ", ");
}
}
// Driver method to test above method
public static void main (String[] args)
{
HashMap<Integer, String> map = new HashMap<Integer, String>();
insertAndPrint(map);
}
}
LinkedHashMap
// Java program to print ordering
// of all elements using LinkedHashMap
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
// This function prints ordering of all elements
static void insertAndPrint(AbstractMap<Integer, String> map)
{
int[] array= {1, -1, 0, 2,-2};
for (int x: array)
{
map.put(x, Integer.toString(x));
}
for (int k: map.keySet())
{
System.out.print(k + ", ");
}
}
// Driver method to test above method
public static void main (String[] args)
{
LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer, String>();
insertAndPrint(map);
}
}
TreeMap
// Java program to print ordering of
// all elements using TreeMap
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
// This function prints ordering of all elements
static void insertAndPrint(AbstractMap<Integer, String> map)
{
int[] array= {1, -1, 0, 2,-2};
for (int x: array)
{
map.put(x, Integer.toString(x));
}
for (int k: map.keySet())
{
System.out.print(k + ", ");
}
}
// Driver method to test above method
public static void main (String[] args)
{
TreeMap<Integer, String> map = new TreeMap<Integer, String>();
insertAndPrint(map);
}
}
Output of HashMap:
-1, 0, 1, -2, 2,
// ordering of the keys is essentially arbitrary (any ordering)
Output of LinkedHashMap:
1, -1, 0, 2, -2,
// Keys are ordered by their insertion order
Output of TreeMap:
-2, -1, 0, 1, 2,
// Keys are in sorted order
Comparison Table
Property HashMap LinkedHashMap TreeMap Time Complexity (Big O) O(1) for Get, Put, ContainsKey, and Remove methods O(1) for Get, Put, ContainsKey, and Remove methods O(log n) for Get, Put, ContainsKey, and Remove methods Iteration Order Random Sorted according to either Insertion Order or Access Order (as specified during construction) Sorted according to either Natural Order of keys or a Comparator (as specified during construction) Null Keys Allowed Allowed Not allowed if keys use Natural Ordering or a Comparator that does not support null keys. Interface Map Map Map, SortedMap, and NavigableMap Synchronization None, use Collections.synchronizedMap() None, use Collections.synchronizedMap() None, use Collections.synchronizedMap() Data Structure List of buckets. If more than 8 entries exist in a bucket, Java 8 switches to a balanced tree from a linked list. Doubly Linked List of Buckets Red-Black Tree (a self-balancing binary search tree). Offers O(log n) for insert, delete, and search Applications General-purpose, fast retrieval, non-synchronized. Use ConcurrentHashMap for concurrency. Useful for LRU Cache or when insertion/access order matters Algorithms requiring sorted or navigable features (e.g., range search, finding nearest values, etc.) Requirements for Keys equals() and hashCode() must be implemented. equals() and hashCode() must be implemented. A Comparator must be supplied for key implementation, or natural ordering will be used
Real Life Applications
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4