Three major collections of Java (three): Map

Map: Disordered (stored in), key unique, and can be stored null
. The main interfaces are: HashMap , HashTable , LinkedHashMap , TreeMap , ConcurrentHashMap

================================================================

HashMap : Based on hashCode(), the bottom layer is an array of Entry key-value pairs

Q: How does HashMap add data? (Judgment mechanism: hashCode+equals())
Answer: According to the hashCode value of the added key modulo the length of the current array (default 16), the result of the operation is the subscript of the key-value pair in the array, and then the key-value pair is saved. So unordered.
If the subscripts obtained after the key modulo length are equal, this situation is called hash collision (hash collision). At
this time, continue to judge the equals method of the key. If equals is judged to be true, the original key will be changed. If it is false, a linked list (put) will be
formed after the original key-value pair at the subscript. After jdk1.8, if the length of the linked list reaches 8, a red-black tree will be formed.
When the delete operation is performed, when the remaining number of key-value pairs in the linked list is less than or equal to 6, the red-black tree is degenerated into a linked list again.

Question: So how to resolve hash conflicts?
Answer: Design the array reasonably (of course, the longer the array, the lower the probability of hash collision, it depends on whether you are a local tyrant)

===================================================================

Hashtable : It is a thread-safe HashMap that cannot store null keys and null values

Properties: It is a subclass of Hashtable, but the key-values ​​are all String types, which are generally used as configuration files: .xml, .properties, .ini/.init, .yml (SprintBoot), so as to solve the hard-coding problem of files

ConcurrentHashMap: It is a thread-safe and more efficient HashTable, theoretically 16 times that of Hashtable. JDK1.5 started
LinkedHashMap: It is a HashMap that maintains a linked list internally, which can guarantee the order of deposit and withdrawal

=======================================================================

TreeMap : The rules for judging key duplication are the same as TreeSet
. The keys in TreeMap either have natural sorting capabilities (keys need to implement the Comparable interface and override the CompareTo method); or TreeMap provides a comparator implementation class object.

Sorting rules: The return value is 0: the key is repeated. If it
is a positive number, it will be sorted according to the ascending order
of the key. It is a negative number, and the descending order

================================================= ============
Three types of values ​​for Map
entrySet(): take the key-value pair
keySet(): take only the key
values(): take only the value

		//3种遍历方法
		//2种遍历方式:foreach和迭代器
		Set<Entry<String, Integer>> entrySet = hashMap.entrySet();
		Iterator<Entry<String, Integer>> iterator = entrySet.iterator();
		while (iterator.hasNext()) {
    
    
			Map.Entry<java.lang.String, java.lang.Integer> entry = (Map.Entry<java.lang.String, java.lang.Integer>) iterator
					.next();
			System.out.println(entry.getKey()+"----------"+entry.getValue());
			//System.out.println(iterator.next());
		}
		
		for (Entry<String, Integer> entry : entrySet) {
    
    //效率最高,因为一次遍历就把键和值都取出来了
			System.out.println(entry.getKey()+" = "+entry.getValue());
		}

		Set<String> keySet = hashMap.keySet();//二次查询
		for (String string : keySet) {
    
    
			System.out.println(string+" = "+hashMap.get(string));
		}
		
		Collection<Integer> values = hashMap.values();//只能取值
		for (Integer integer : values) {
    
    
			System.out.println(integer);
		}

==============================================================
Insert picture description here

Guess you like

Origin blog.csdn.net/ExceptionCoder/article/details/107683570