java. Map usage pay attention to details

When we add an element to a set, HashMap, HashSet, and HashTable collection, the collection will first call the hashCode method of the object,
so that the location where it is stored can be directly located. If there are no other elements there, it will be saved directly.
If there is already an element there, call the equals method to match whether the two elements are the same, if they are the same, they do not exist, and if they are different, they are hashed to other locations (that is, re-hash the value according to the hashed value, similar to executing a Recursion)


Treemap key values ​​are arranged in order, and the key storage location of hashmap is determined by hashcode, so it is out of order.


When hashmap requires concurrent access:
Map m = Collections.synchronizedMap(new HashMap(…));


At the same time, pay attention to the performance-related details of hashMap use

public static void main(String[] args) {
		Map<Integer,Object> map = new HashMap<>();
		Date d = new Date();
		for (int i = 0; i < 10000000; i++) {
			map.put(i, null);
		}
		Date d1 = new Date();
		System.out.println(d1.getTime()-d.getTime());

		Map<Integer,Object> map1 = new HashMap<>(10000000);
		for (int i = 0; i < 10000000; i++) {
			map1.put(i, null);
		}
		Date d2 = new Date();
		System.out.println(d2.getTime()-d1.getTime());
	}


When using HashMap, if the initial capacity is not provided in advance, each time the capacity is exceeded and the capacity is automatically expanded, since the key value is determined according to the hash value, it is necessary to perform a rehash operation on the key value. It leads to a lot of memory consumption, so it is recommended to set the initial capacity correctly when using hashmap.



Running results:
5897
2694



To summarize the use of map,

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 is mainly used to store key-value pairs and obtain values ​​according to keys , so duplicate keys are not allowed (duplicate overwrites), but duplicate values ​​are allowed.

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, the synchronizedMap method of Collections can be used to enable HashMap to be synchronized, or ConcurrentHashMap can be used.
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 is a subclass of HashMap, which saves the insertion order of records. When using Iterator to traverse LinkedHashMap, the first record 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. To insert, delete and locate elements in Map, HashMap is the best choice. But if you want to iterate over keys in natural order or custom order then TreeMap is better. If the order of output is the same as that of input, LinkedHashMap can be used to achieve it, and it can also be arranged in the order of reading.

HashMap is one of the most commonly used Maps, which stores data according to the hashCode value of the key, and can directly obtain its data according to the key. value, with very fast access speed. HashMap only allows the key of one record to be NULL at most, and 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, which may lead to data inconsistency. If synchronization is required, HashMap can be synchronized with the synchronizedMap method of Collections.

Hashtable is similar to HashMap, except that it does not allow the keys or values ​​of records to be empty; it supports thread synchronization, that is, only one thread can write to Hashtable at any time, which also causes Hashtable to be slower when writing.
LinkedHashMap saves the insertion order of records. When using Iterator to traverse LinkedHashMap, the record obtained first must be inserted first.

It will be slower than HashMap when traversing. TreeMap can sort the records it saves according to the key. The default is to sort in ascending order, and you can also specify a sort comparator. When traversing the TreeMap with an Iterator, the resulting records are sorted.


When programming concurrently, use copyonwritearraylist concurrenthashmap to optimize multi-threaded reading and writing
When performance requirements, using weakashmap will automatically gc map

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326724091&siteId=291194637