Last Updated: 22 January, 2023
HashMap and LinkedHashMap are available in the java.util package, both of which implement the Map Interface and accept the data in the form of key-value pairs. Both have almost the same behaviour because LinkedHashMap is a child class of HashMap.
There are many differences between the HashMap and TreeMap that are given below.
The HashMap class was introduced in the Java 1.2 version and is available in the java.util package. HashMap is a child class of the AbstractMap class, and it implements the Map interface as well as the Cloneable and Serializable interfaces.
HashMap is used to store the data in the form of key-value pairs, where keys must be unique and values may be duplicated. HashMap accepts only one null key and multiple null values.
Java HashMap Class Declaration
Here, K is the key Object type and V is the value Object type.
Java HashMap important features:
The LinkedHashMap class is a child class of HashMap. It was introduced in the Java 1.4 version and is available in the java.util package.
A LinkedHashMap is used to store the data in the form of key-value pairs. The implementation of LinkedHashMap is based on hash table and linked list.
LinkedHashMap maintains the insertion order of data.
Java LinkedHashMap Class Declaration
Here, K is the key Object type and V is the value Object type.
Java LinkedHashMap important features:
HashMap | LinkedHashMap |
---|---|
HashMap extends the AbstractMap class. | LinkedHashMap extends the HashMap class. |
HashMap does not maintain the insertion order of elements. | The insertion order of elements is preserved by LinkedHashMap. (Major Difference) |
HashMap uses hash tables to store maps. | LinkedHashMap uses a hash table along with a linked list to store maps. |
HashMap was introduced in the JDK 1.2 version. | LinkedHashMap was introduced in the JDK 1.4 version. |
HashMap required less memory than LinkedHashMap. | LinkedHashMap required more memory than LinkedHashMap. |
HashMap gives faster performance than LinkedHashMap. | LinkedHashMap's performance is a bit slower than HashMap's. |
Output
Iterating Hashmap and printing Key and Value
in - India
en - England
us - United State
ca - Canada
Output
Iterating LinkedHashMap and printing Key and Value
in - India
us - United State
en - England
ca - Canada
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com