HashMap, TreeMap, LinkedHashMap, Hashtable

HashMap, TreeMap, LinkedHashMap, Hashtable

The Map interface in the Java programming language is an important feature of the Java built-in collections framework. It represents a collection of key-value pairs and a sequence of ordered elements to the Java user. Each key-value pair has a unique property, and each user-defined key corresponds to exactly one value. Key-value pairs are four important concepts commonly used in the Map interface.

HashMap, TreeMap, LinkedHashMap, Hashtable

Elements of the Java Map interface.

  • HashMap
  • TreeMap
  • LinkedHashMap
  • Hashtable

Each class defined here has its own special features and functions, and their performance impact and usage methods and procedures may vary.

So let’s better understand the concepts of HashMap, TreeMap, LinkedHashMap, and Hashtable in Java programming.

HashMap in Java.

The HashMap interface in the Java language is a popular method for implementing common and widely used concepts. The HashMap concept allows Java users to use the hash table method to store the ordered structure of key-value pairs, and allows the use of defined keys and values ​​in a HashMap to be null.

Important Features of a Java HashMap.

  • No Ordering – It does not provide any specific order arrangement for HashMap entries. Java users can modify the order by adding or removing new elements from the HashMap data type.
  • Allows one null and any number of null values ​​- HashMap allows Java programmers to create a null key and multiple null values ​​in a program.
  • Efficiency – HashMap provides Java users with constant-time complexity (O(1)) for basic program operations like get and put, by treating the HashMap as if the hash function propagates the keys in a properly ordered sequence.
  • Not synchronized – Remember, by default, this is not thread-safe in Java. If Java programmers need to synchronize it, Java users can use external synchronization.

Example of a Java HashMap.

import java.util.HashMap;

public class HashMapDataType {

public static void main(String[] args) {

HashMap<String, Integer> map = new HashMap<>();

// Here we are adding a new key-value pair to the HashMap

map.put(“Macbook”, 2);

map.put(“Imac”, 3);

map.put(“Hp Spectre”, 1);

map.put(“Asus Rog”, 4);

map.put(“Macbook”, 5); // here This will overwrite the previous value of “Macbook” hash element

// here it Iterating through the HashMap

for (String key : map.keySet()) {

System.out.println(key + “: ” + map.get(key));

}

//here it Checking if a key exists or not

if (map.containsKey(“Imac”)) {

System.out.println(“Here Imac is available in the map”);

}

//here we Removing a key-value pair

map.remove(“Hp Spectre”);

System.out.println(“After HashMap deletion – ” + map);

}

}

TreeMap in Java.

In the Java language, TreeMap is an important feature or data type representation method of the Map interface. The TreeMap data type follows the Red-Black Tree (a self-balancing binary search tree) concept. Here, the TreeMap data type stores or maintains keys in a sorted order according to their natural arrangement or according to the Comparator provided at the time of TreeMap element creation.

Important Features of Java TreeMap.

  • Sorted – A TreeMap in a Java program stores key information in an increasing sequence order by default, for comparative key data value analysis.
  • Logarithmic Time Complexity – Program operations such as add, remove, and get in a Java TreeMap are defined to have a time complexity of O(log n), which is represented by the tree structure within it.
  • No Null Keys – Remember, TreeMap in Java does not allow null keys, while it does allow null values.
  • Not Synchronized – Unlike the HashMap list data type in Java programs, TreeMap does not provide synchronized features.

Instance of Java TreeMap.

import java.util.TreeMap;

public class TreeMapDataType {

public static void main(String[] args) {

TreeMap<String, Integer> map = new TreeMap<>();

// Here we are adding a new TreeMap key-value pair

map.put(“Macbook”, 2);

map.put(“Imac”, 3);

map.put(“Hp Spectre”, 1);

map.put(“Asus Rog”, 4);

map.put(“Asus Tuf”, 5);

// here it Iterating through the TreeMap/sorted order manner

for (String key : map.keySet()) {

System.out.println(key + “: ” + map.get(key)); // the result will be in sorted order

}

// here it Checking if a key exists r not

if (map.containsKey(“Imac”)) {

System.out.println(“here Imac model available in the map”);

}

// here it Removing a key-value pair

map.remove(“Hp Spectre”);

System.out.println(“After TreeMap element deletion – ” + map);

}

}

LinkedHashMap in Java.

In Java programming, LinkedHashMap is an idempotent data information representation method of the Map interface data type, which connects a hash table with a linked list. LinkedHashMap maintains the insertion sequence order of keys. This means that the order of LinkedHashMap elements remains the same as the order in which they were created, i.e., when they were added to the LinkedHashMap map by the user.

Important Features of LinkedHashMap in Java.

  • Insertion Order – Unlike HashMap data maps, LinkedHashMap maintains the order of keys based on the insertion order of data values.
  • Efficiency – LinkedHashMap provides constant-time complexity (O(1)) features for basic program operations such as HashMap. However, LinkedHashMap also incurs additional overhead to maintain the data element insertion order.
  • Allows Null Keys and Values ​​- Like the HashMap data type, LinkedHashMap allows Java users to insert a single null key and multiple null values.
  • Not Synchronized – Unlike HashMap and TreeMap, LinkedHashMap does not have synchronized features in Java programs.

Java LinkedHashMap Example.

import java.util.TreeMap;

public class TreeMapDataType {

public static void main(String[] args) {

TreeMap<String, Integer> map = new TreeMap<>();

// Here we are adding new TreeMap key-value pairs

map.put(“Macbook”, 2);

map.put(“Imac”, 3);

map.put(“Hp Spectre”, 1);

map.put(“Asus Rog”, 4);

map.put(“Asus Tuf”, 5);

// here it Iterating through the TreeMap/sorted order manner

for (String key : map.keySet()) {

System.out.println(key + “: ” + map.get(key)); // the result will be in sorted order

}

//here it Checking if a key exists or not

if (map.containsKey(“Imac”)) {

System.out.println(“here Imac model available in the map”);

}

// here it Removing a key-value pair

map.remove(“Hp Spectre”);

System.out.println(“After TreeMap element deletion – ” + map);

}

}

Hashtable in Java.

In Java programming, Hashtable is an idempotent data information representation method of an old data type of the Map interface. It is similar to the HashMap data type, but has some differences. Hashtable uses a hash table to store key-value pairs in Java programs, and was a built-in part of the original version of Java programming.

Important Features of Java Hashtable.

  • Synchronized – Unlike the Java HashMap data type, Hashtable is synchronized in nature, so it is a thread-safe method. However, it tends to be slow in single-threaded applications.
  • No Null Keys or Values ​​- Unlike HashMap and LinkedHashMap, Java Hashtable allows null keys or values.
  • Legacy Class – Hashtable is treated as a legacy class in Java, and it is generally not recommended for Java users to use it in modern Java applications unless they have special thread safety requirements.
  • Slow Performance – Due to synchronization overhead in Java, Hashtable generally performs slower than HashMap and LinkedHashMap.

Example of Java Hashtable.

import java.util.Hashtable;

public class HashtableDataType {

public static void main(String[] args) {

Hashtable<String, Integer> map = new Hashtable<>();

// Here we are adding a new key-value pair to the Hashtable

map.put(“Macbook”, 2);

map.put(“Imac”, 3);

map.put(“Hp Spectre”, 1);

map.put(“Asus Rog”, 4);

map.put(“Asus Tuf”, 5);

// here it Iterating through the Hashtable element

for (String key : map.keySet()) {

System.out.println(key + “: ” + map.get(key));

}

// here it Checking if a key exists in Hashtable

if (map.containsKey(“Imac”)) {

System.out.println(“Imac is available in the Hashtable map”);

}

// here we Removing a key-value pair in Hashtable

map.remove(“Hp Spectre”);

System.out.println(“After deletion element in Hashtable – ” + map);

}

}

Difference between HashMap, TreeMap, LinkedHashMap, and Hashtable.

Each FeatureHashMap data typeTreeMap data typeLinkedHashMap data typeHashtable data type
Its OrderingHashMap data type No following any ordering default unordered statusTreeMap data type use Sorted method ascending sequence orderLinkedHashMap data type follow Insertion order methodHashtable data type follow No ordering unordered data structure
Null Keys/Values availabilityHashMap Allows user to use one null key and multiple null values in data element when needTreeMap Does not allow any null keys, but it allows null values whenever user needLinkedHashMap Allows one null key and multiple null values whenever user needHashtable Does not allow null keys or values in program
Thread-Safety featuresHashMap does Not support synchronized featuresTreeMap Does not allow synchronized conceptLinkedHashMap Not allow synchronized methodHashtable allow Synchronized for (thread-safe) features
Time Complexity in useHashMap is O(1) (average case) according to useTreeMap use O(log n) time complexity featuresLinkedHashMap usages O(1) (average case)Hashtable usages O(1) (average case)
Where to Use CaseHashMap is Best for fast data lookups, ew data insertions, and remove existing data without concern for orderTreeMap Best for storing sorted keys and fast searching data valueLinkedHashMap Best choice when new data insertion order is essentialHashtable usages When thread-safety is essential though ConcurrentHashMap is a better alternative method
System PerformanceHashMap Faster than TreeMap and Hashtable map data typeTreeMap data type is Slower than HashMap and LinkedHashMap due to sorting orderLinkedHashMap is Slightly slower than HashMap due to maintaining insertion data element orderHashtable is Slower than HashMap data type due to synchronization overhead features

Java HashMap, TreeMap, LinkedHashMap, and Hashtable Summary.

  • HashMap – In Java programming, HashMap is used when the Java user needs a fast, unordered key map version of key-value pairs and does not require thread safety.
  • TreeMap – In Java programming, TreeMap is used when the Java user needs to display TreeMap keys sorted in a specific order sequence, or when the Java user needs to perform program operations based on the sequence of TreeMap keys.
  • LinkedHashMap – In Java programming, LinkedHashMap is used when the Java user needs to maintain the insertion order of new data elements and requires fast data access, removal, and insertion operations.
  • Hashtable – In Java programming, Hashtable is used when you have a specific need for thread-safety and newer options like ConcurrentHashMap are not applicable. Hashtable is obsolete in Java programming today, and Hashtable is not recommended in new large Java projects.

Leave a Reply