Map interface and its implementation class

Posted by peterjoel on Sun, 30 Jan 2022 12:44:07 +0100

Map interface

Map provides a mapping relationship, in which the elements are stored in the form of key value pairs, which can quickly find the value according to the key;
Key value pairs in Map exist in the form of object instances of Entry type;
The key value cannot be repeated. The value value can be repeated. A value value can form a corresponding relationship with many key values. Each value can only be mapped to one value at most.
Map supports generics, such as map < K, V >
Use the put(K key,V value) method to add in the Map

Common methods defined in Map interface
The specific use is discussed in the implementation class

//		HashMap<String, Student> hm = new HashMap<String, Student>();
//		hm.put("2018050401", new Student("2018050401", "Zhang San", 18, 80.0));
//		hm.put("2018050402", new Student("2018050402", "Li Si", 18, 80.0));
//		hm.put("2018050403", new Student("2018050403", "Li Si", 18, 80.0));
//		hm.put("2018050404", new Student("2018050404", "Wang Wu", 18, 80.0));
//		hm.put("2018050404", new Student("2018050404", "Wang Wu", 18, 80.0));
//		
//		//Method 1: find value through key
//		Set<String> keys = hm.keySet();
//		for (String key : keys) {
//			Student s = hm.get(key);
//			System.out.println(key + "|" + s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore());
//		}

		HashMap<Student, String> hm = new HashMap<Student, String>();
		hm.put(new Student("2018050401", "Zhang San", 18, 80.0),"2018050401");
		hm.put(new Student("2018050402", "Li Si", 18, 80.0),"2018050402");
		hm.put(new Student("2018050403", "Li Si", 18, 80.0), "2018050403");
		hm.put(new Student("2018050404", "Wang Wu", 18, 80.0), "2018050404");
		hm.put(new Student("2018050404", "Wang Wu", 18, 80.0), "2018050404");
		
		// Method 2: find the key and value of the object through the key and value
		Set<Entry<Student, String>> keyValues = hm.entrySet();
		for (Entry<Student, String> keyValue : keyValues) {
			Student s = keyValue.getKey();
			String value = keyValue.getValue();
			System.out.println(s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore() + "=" + value);
		}

LinkedHashMap

The hash table and linked list implementation of Map interface has a predictable iterative order
characteristic:
Keys are ordered, unique,
Values are ordered and repeatable, similar to List
The underlying data structure is a hash table and a linked list. The hash table ensures that the keys are unique and the linked list ensures that the keys are orderly

		LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>();
		lhm.put(01, "Zhang san1");
		lhm.put(02, "Zhang San 2");
		lhm.put(03, "Zhang SAN3");
		lhm.put(04, "Zhang San 4");
		lhm.put(05, "Zhang San 5");
		
		Set<Integer> keys = lhm.keySet();
		for (Integer key : keys) {
			System.out.println(key + "|" + lhm.get(key));
		}

TreeMap

Implementation of NavigableMap based on red black tree. The mapping is sorted according to the natural order of its keys, or according to the Comparator provided when creating the mapping,
It depends on the construction method used.
characteristic:
Key sortable, unique,
Values are ordered and repeatable, similar to List
The underlying data structure is a self balanced binary tree, which can be sorted
The sorting method is similar to TreeSet, which is divided into natural sorting and comparator sorting, depending on the construction method used

		TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
		tm.put(24, "Hello1");
		tm.put(14, "Hello2");
		tm.put(34, "Hello3");
		tm.put(124, "Hello4");
		tm.put(24, "Hello5");
		tm.put(24, "Hello6");
		tm.put(24, "Hello7");
		tm.put(244, "Hello8");
		tm.put(624, "Hello9");
		tm.put(24, "Hello10");
		Set<Integer> keys = tm.keySet();
		for (Integer key : keys) {
			String value = tm.get(key);
			System.out.println(key + "|" + value);
		}

HashTable

This class implements a hash table that maps keys to corresponding values. Any non null object can be used as a key or value
characteristic:
Null keys and null values are not allowed
Thread safety and low efficiency
Differences between HashMap and Hashtable:

HashMap It is unsafe, asynchronous and efficient to allow null Key sum null value
Hashtable It is safe and inefficient synchronization is not allowed null Key sum null value
 The bottom layer is hash table structure
Hashtable<String, String> hashtable = new Hashtable<String, String>();
		hashtable.put("Liu Bei", "Sun Shangxiang");
		hashtable.put("Sun CE", "Big Joe");
		hashtable.put("Zhou Yu", "Little Joe");
		hashtable.put("Lv Bu", "army officer's hat ornaments");
		System.out.println(hashtable);
		Enumeration<String> keys = hashtable.keys();
		while (keys.hasMoreElements()) {
			String key = keys.nextElement();
			String value = hashtable.get(key);
			System.out.println(key + "|" + value);
		}

WeakHashMap

Hash table based Map implemented with weak key. In WeakHashMap, when a key is no longer used normally, its entry is automatically removed. More precisely, for a given key, the existence of its mapping does not prevent the garbage collector from discarding the key, which makes the key terminatable, terminated, and then recycled.
When a key is discarded, its entry is effectively removed from the Map, so the behavior of this class is different from that of other Map implementations.

		WeakHashMap<String,String> whm = new WeakHashMap<>();
		whm.put(new String("hello1"), "world1");
		whm.put(new String("hello2"), "world2");
		whm.put(new String("hello3"), "world3");
		whm.put("hello4", "world3");
		System.out.println(whm);
		System.gc();
		System.runFinalization();
		System.out.println(whm);

EnumMap

Key is an enumeration type

		EnumMap<Direction, String> em = new EnumMap<>(Direction.class);
		em.put(Direction.UP, "Move up");
		em.put(Direction.DOWN, "Move down");
		em.put(Direction.LEFT, "Move left");
		em.put(Direction.RIGHT, "Move right");
		
		Set<Direction> keys = em.keySet();
		for (Direction key : keys) {
			String value = em.get(key);
			System.out.println(key + "|" + value);
		}