Java double instance collection map HashMap treemap

1.Map interface

The Map interface defines the storage characteristics of the double instance Collection. It is not a sub interface of the Collection interface. The storage feature of double instance set is that it is stored in key and value structures. It embodies the concept of function y=f(x) in mathematics.

The difference between Map and Collection

  • The container elements in the Collection exist in isolation. The elements stored in the Collection are stored one by one

  • The container elements in the Map exist in pairs. Each element consists of a key and a value. The corresponding value can be found through the key

  • Containers in the Collection are called single column collections, and containers in the Map are called double column collections

  • The collection in the Map cannot contain duplicate keys. Values can be repeated. Each key can only correspond to one value

  • The commonly used containers in Map are HashMap and TreeMap

common method

methodexplain
V put(K key,V value)Add key and value to the Map set and return value
void putAll(Map m)Copy all mapping relationships from the specified Map to this Map
V remove(Object key)Delete the value corresponding to the key
V get(Object key)Obtain the corresponding value according to the specified key
boolean containsKey(Object key)Judge whether the container contains the specified key
boolean containsValue(Object value)Determines whether the container contains the specified value
Set keySet()Get all the key s in the Map collection and store them in the Set collection
Set<Map.Entry<K,V>> entrySet()Returns a Set that contains all the mappings in the Map based on the Map.Entry type
void clear()Delete all mappings in the Map

2.HashMap container class

HashMap is the interface implementation class of Map interface, which is implemented by hash algorithm. It is the most commonly used implementation class of Map interface. Because the underlying layer uses hash table to store data, it is required that the key cannot be repeated. If it is repeated, the new value will replace the old value. HashMap has very high efficiency in finding, deleting and modifying.

import java.util.*;
public class HashMapTest {
	public static void main(String[] args) {
		//Instantiate HashMap container
		Map<String,String> map = new HashMap<>();
		//1. Add the element and put the feature. If you encounter an element with the same key, the old one will be overwritten with the new one.
		//If the return value is overwritten, the old value will be returned, otherwise it will be null
		map.put("a", "A");
		String value = map.put("a", "B");
		System.out.println(value);
		//2. The first way to obtain elements is through keys. Disadvantages: if you want to obtain all value s in the Map, you need to know all keys. It's too troublesome
		//It needs to be called many times, okay
		System.out.println("----------------");
		String val = map.get("a");
		System.out.println(val);
		//The second method is to cooperate with the get method through keySet
		//keySet combines all the key s in the Map into one set
		System.out.println("----------------");
		map.put("b", "B");
		map.put("c", "C");
		map.put("d", "D");
		Set<String> keys = map.keySet();
		for(String key:keys) {
			String v1 = map.get(key);
			System.out.println(key+"--"+v1);
		}
		//The third way is to obtain the Map.Entry type element through the entrySet method
		System.out.println("----------------");
		//Entry is an internal interface. Through the external interface. Internal interface, the key value pairs are scattered and put into the Set
		Set<Map.Entry<String,String>> entrySet = map.entrySet();
		//Traversal set
		for(Map.Entry<String,String> entry:entrySet) {
			String key = entry.getKey();
			String v = entry.getValue();
			System.out.println(key+"----"+v);
		}
		//3. For the union operation of map container, the generic types must be the same
		System.out.println("----------------");
		
		Map<String,String> map1 = new HashMap<>();
		map1.put("f", "F");
		//Note that the value of key is c
		map1.put("c", "cc");
		map1.putAll(map);
		Set<String> keys2 = map1.keySet();
		//The result here is map1 and map
		for(String key:keys2) {
			String v2= map1.get(key);
			System.out.println(key+"--"+v2);
		}
		//4. Delete elements from map container
		System.out.println("----------------");
		String val1 = map.remove("d");
		Set<String> set2 = map.keySet();
		for(String key:set2) {
			String v2 = map.get(key);
			System.out.println(key+"-----"+v2);
		}
		//5. Judge whether Key and value exist
		System.out.println("----------------");
		boolean flag1 = map.containsKey("aaaa");
		System.out.println(flag1);
		boolean flag2 = map.containsKey("a");
		System.out.println(flag2);
		boolean flag3 = map.containsValue("B");
		System.out.println(flag3);
		boolean flag4 = map.containsValue("BB");
		System.out.println(flag4);
	}

}
/*
A
----------------
B
----------------
a--B
b--B
c--C
d--D
----------------
a----B
b----B
c----C
d----D
----------------
a--B
b--B
c--C
d--D
f--F
----------------
a-----B
b-----B
c-----C
----------------
false
true
true
false
*/

3. Underlying implementation of HashMap

Underlying implementation and source code analysis of HashMap in Java_ lipengfei0427 blog - CSDN blog

Continue with the previous article

4.TreeMap container class

TreeMap and HashMap also implement the Map interface. Therefore, there is no difference in the usage of API. The efficiency of HashMap is higher than that of TreeMap. TreeMap is a container that can sort keys. TreeMap can be used when sorting keys. The bottom layer of TreeMap is based on red black tree.

When using TreeMap, you need to give a collation:

  • The element itself implements comparison rules

  • Comparison rules are implemented through comparators

//Self implementation comparison rules. There are notes on the Users class. Turn it over
import java.util.*;
public class TreeMapTest {
	public static void main(String[] args) {
		//Instantiate TreeMap
		Map<Users,String> map = new TreeMap<>();
		Users u1 = new Users("li1",3);
		Users u2 = new Users("li2",4);
		map.put(u1,"li1");
		map.put(u2, "li2");
		Set<Users> keys = map.keySet();
		for(Users key:keys) {
			System.out.println(key+"---"+map.get(key));
		}
	}
}
//The comparison rules are implemented through the comparator. Both the Student class and the StudentComparator comparator have them. Turn it over
import java.util.*;
public class TreeMapTest02 {
	public static void main(String[] args) {
		Map<Student,String> treemap = new TreeMap<>(new StudentComparator());
		Student s1 = new Student("li1",18);
		Student s2 = new Student("li2",22);
		Student s3 = new Student("li1.5",22);
		treemap.put(s1,"li1");
		treemap.put(s2, "li2");
		treemap.put(s3,"li1.5");
		Set<Student> keys = treemap.keySet();
		for(Student key:keys) {
			System.out.println(key+"---"+treemap.get(key));
		}

	}
	
}

Tags: Java HashMap map treemap

Posted on Thu, 25 Nov 2021 16:07:38 -0500 by freddy-a