- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between TreeMap, HashMap, and LinkedHashMap in Java
HashMap, TreeMap and LinkedHashMap all implements java.util.Map interface and following are their characteristics.
HashMap
HashMap has complexity of O(1) for insertion and lookup.
HashMap allows one null key and multiple null values.
HashMap does not maintain any order.
TreeMap
TreeMap has complexity of O(logN) for insertion and lookup.
TreeMap does not allow null key but allow multiple null values.
TreeMap maintains order. It stores keys in sorted and ascending order.
LinkedHashMap
LinkedHashMap has complexity of O(1) for insertion and lookup.
LinkedHashMap allows one null key and multiple null values.
LinkedHashMap maintains order in which key-value pairs are inserted.
Example
import java.util.HashMap; import java.util.Hashtable; import java.util.LinkedHashMap; import java.util.Map; import java.util.TreeMap; public class Tester { public static void main(String args[]) { Map<String, String> map = new HashMap<String, String>(); map.put("One", "1"); map.put("Five", "5"); map.put("Four", "4"); map.put("Two", "2"); map.put("Three", "3"); System.out.println("HashMap:
" + map); Map<String, String> map1 = new LinkedHashMap<String, String>(); map1.put("One", "1"); map1.put("Five", "5"); map1.put("Four", "4"); map1.put("Two", "2"); map1.put("Three", "3"); System.out.println("LinkedHashMap:
" + map1); Map<String, String> map2 = new TreeMap<String, String>(); map2.put("One", "1"); map2.put("Five", "5"); map2.put("Four", "4"); map2.put("Two", "2"); map2.put("Three", "3"); System.out.println("TreeMap:
" + map2); } }
Output
HashMap: {Five = 5, One = 1, Four = 4, Two = 2, Three = 3} LinkedHashMap: {One = 1, Five = 5, Four = 4, Two = 2, Three = 3} TreeMap: {Five = 5, Four = 4, One = 1, Three = 3, Two = 2}
Here you see, HashMap has random order of Keys, LinkedHashMap has preserved the order in which keys are inserted and TreeMap has sorted order of keys.
- Related Articles
- Difference between TreeMap, HashMap and LinkedHashMap in Java programming
- Differences between TreeMap, HashMap and LinkedHashMap in Java
- What is the differences between TreeMap, HashMap and LinkedHashMap in Java?
- Difference Between HashMap and LinkedHashMap in Java
- Difference Between HashMap and TreeMap in Java
- How to implement HashMap, LinkedHashMap, and TreeMap in JShell in Java 9?
- Difference between HashMap and HashSet in Java.
- Difference between EnumMap and HashMap in Java
- Difference between HashMap and HashTable in Java.
- Difference between HashMap and ConcurrentHashMap in Java
- Difference between HashTable and HashMap in Java
- Program to convert HashMap to TreeMap in Java
- Java Program to convert HashMap to TreeMap
- Difference between Concurrent hash map and Synchronized hashmap in Java
- Difference between HashMap and ConcurrentHashMap
