- 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
What is the differences between TreeMap, HashMap and LinkedHashMap in Java?
HashSet and ArrayList both are one of the most important classes of the Java Collection framework.
The following are the important differences between TreeMap, HashMap and LinkedHashMap.
Sr. No. | Key | TreeMap | HashMap | LinkedHashMap |
---|---|---|---|---|
1 | Ordering of elements | The elements inserted in TreeMap are sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. | In case of HashMap it does not guarantees as to the order of the map also it does not guarantee that the order will remain constant over time. | LinkedHashMap follows the insertion order of elements and also maintaining an order of elements inserted into it. |
2 | Internal implementation | The internal implementation of TreeMap does not allow storing null key but only null values are allowed. | In the case of HashMap storing one null key as well as of multiple null values are allowed. | LinkedHashmap is internally implemented more similar as HashMap so storing of one null key and of multiple null values are allowed. |
3 | Operational Complexity | TreeMap comes with the complexity of its get,put and remove operations as O(log(n)), which is greater than that of HashMap | HashMap on other hand has the complexity of O(1) in case of its get,put and remove operations. | LinkedHashMap again has the same complexity as of HashMap i.e O(1). |
4 | Inheritance | TreeMap implements SortedMap interface of Collection framework which is a child of Map.And internally TreeMap implements Red-Black Tree(a Self Balancing Binary Search Tree). | On other hand HashMap implements simple Map interface and internally uses hashing for storing and retrieval of its elements. | Alike of TreeMap LinkedHashMap extends HashMap and internally uses hashing as like in HashMap. |
5 | Index performance | TreeMap is maintaining order of its elements hence is lesser in performance index and also requires more memory than HashMap and LinkedHashMap. | HashMap as do not maintain any insertion order of its elements hence is faster as compare to TreeMap also do not sort its elements on the basis of its value so also faster than LinkedHashMap. | LinkedHashMap is faster as compare to TreeMap but is slower than HashMap. |
6 | Comparison | Elements in TreeMap get compared by using compareTo() method in which custom implementation could also be provided. | On other hand HashMap uses compare() method of Object class for its elements comparison. | LinkedHashMap also uses compare() method of Object class for its elements comparison. |
Example of TreeMap vs HashMap vs LinkedHashMap
JavaTester.java
// Java program to print ordering // of all elements using HashMap import java.util.*; import java.lang.*; import java.io.*; public class JavaTester{ static void getAndShow(AbstractMap map){ int[] input= {1, -1, 0, 2,-2}; for (int x: input){ map.put(x, Integer.toString(x)); } Set<Integer> keySet = map.keySet(); for (int k: keySet){ System.out.print(k + ", "); } } public static void main (String[] args){ HashMap map = new HashMap(); getAndShow(map); LinkedHashMap map1 = new LinkedHashMap(); getAndShow(map1); TreeMap map2 = new TreeMap(); getAndShow(map2); } }
Output
-1, 0, 1, -2, 2, 1, -1, 0, 2, -2, -2, -1, 0, 1, 2,
- Related Articles
- Differences between TreeMap, HashMap and LinkedHashMap in Java
- Difference between TreeMap, HashMap, and LinkedHashMap in Java
- Difference between TreeMap, HashMap and LinkedHashMap in Java programming
- How to implement HashMap, LinkedHashMap, and TreeMap in JShell in Java 9?
- Difference Between HashMap and LinkedHashMap in Java
- Difference Between HashMap and TreeMap in Java
- What is the differences between HashMap and HashTable in Java
- Differences between HashMap and Hashtable in Java
- Program to convert HashMap to TreeMap in Java
- Java Program to convert HashMap to TreeMap
- 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

Advertisements