The difference between HashMap, LinkedHashMap, TreeMap in Java

Java defines an interface java.util.Map for the mapping in the data structure; it has four implementation classes, namely HashMap Hashtable LinkedHashMap and TreeMap
Map are mainly used to store key-value pairs, and get values ​​according to keys, so key duplication is not allowed (duplicate overrides), but allow value duplication.
Hashmap is one of the most commonly used Maps. It stores data according to the HashCode value of the key, and its value can be obtained directly according to the key. It has a fast access speed. When traversing, the order of obtaining data is completely random. HashMap only allows the key of one record to be Null at most; allows the value of multiple records to be Null; HashMap does not support thread synchronization, that is, multiple threads can write HashMap at the same time at any time; it may cause data inconsistency. If synchronization is required, you can use the synchronizedMap method of Collections to enable HashMap to be synchronized, or use ConcurrentHashMap.
Hashtable is similar to HashMap, it inherits from Dictionary class, the difference is: it does not allow the key or value of records to be empty; it supports thread synchronization, that is, only one thread can write Hashtable at any time, so it also causes Hashtable to write Entry will be slower.
LinkedHashMap saves the insertion order of records. When using Iterator to traverse LinkedHashMap, the record obtained first must be inserted first. You can also use parameters during construction to sort according to the number of applications. It will be slower than HashMap when traversing, but there is an exception. When the HashMap has a large capacity and the actual data is small, the traversal may be slower than LinkedHashMap, because the traversal speed of LinkedHashMap is only related to the actual data, and has nothing to do with the capacity. The traversal speed of HashMap is related to its capacity.
TreeMap implements the SortMap interface, which can sort the records it saves according to the key. The default is to sort in ascending order of the key value. You can also specify the sort comparator. When iterator is used to traverse the TreeMap, the obtained records are sorted.

In general, we use HashMap the most. The key-value pairs stored in HashMap are random when they are taken out. It stores data according to the HashCode value of the key, and can directly obtain its value according to the key. access speed. Insert, delete and locate elements in Map, HashMap is the best choice.
TreeMap takes out the sorted key-value pairs. But if you want to iterate over keys in natural order or custom order then TreeMap is better.
LinkedHashMap is a subclass of HashMap. If the order of output is the same as that of input, LinkedHashMap can be used. It can also be arranged in the order of reading, which can be used in connection pooling.

The following code example shows the difference between HashMap, LinkedHashMap, and TreeMap:

import java.util.HashMap;  
import java.util.Iterator;  
import java.util.LinkedHashMap;  
import java.util.Map;  
import java.util.TreeMap;  
public class MapTest2{  
	@SuppressWarnings("unchecked")  
	public static void main(String[] args) {    
		//HashMap  
		System.out.println("------HashMap unordered output------");  
		HashMap hsMap=new HashMap();  
		hsMap.put("3", "Value3");  
		hsMap.put("1", "Value1");  
		hsMap.put("2", "Value2");  
		hsMap.put("b", "ValueB");  
		hsMap.put("a", "ValueA");  
		Iterator it = hsMap.entrySet().iterator();  
		while (it.hasNext()) {  
			Map.Entry e = (Map.Entry) it.next();  
			System.out.println("Key: " + e.getKey() + "--Value: "  + e.getValue());  
		}  
	  
		//TreeMap  
		System.out.println("------TreeMap sort output by Key------");  
		TreeMap teMap=new TreeMap();  
		teMap.put("3", "Value3");  
		teMap.put("1", "Value1");  
		teMap.put("2", "Value2");  
		teMap.put("b", "ValueB");  
		teMap.put("a", "ValueA");  
		Iterator tit = teMap.entrySet().iterator();  
		while (tit.hasNext()) {  
			Map.Entry e = (Map.Entry) tit.next();  
			System.out.println("Key: " + e.getKey() + "--Value: "  + e.getValue());  
		}  
	  
		//LinkedHashMap  
		System.out.println("--LinkedHashMap output according to the order of input--");  
		LinkedHashMap lhsMap=new LinkedHashMap();  
		lhsMap.put("3", "Value3");  
		lhsMap.put("1", "Value1");  
		lhsMap.put("2", "Value2");  
		lhsMap.put("b", "ValueB");  
		lhsMap.put("a", "ValueA");  
		Iterator lit = lhsMap.entrySet().iterator();  
		while (lit.hasNext()) {  
			Map.Entry e = (Map.Entry) lit.next();  
			System.out.println("Key: " + e.getKey() + "--Value: "  + e.getValue());  
		}  
	}  
  
}

Original link: https://blog.csdn.net/xiyuan1999/article/details/6198394

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325520141&siteId=291194637