Map bilateral queue in Java

1.1 What is the Map

Key (Key) value (Value) for
Deng Chao = Goddess
Beckham = Victoria
Huang Lei = Sun Li
Wu Jing Xie Nan =

Form:
Name: Joe Smith
Age: 23
Gender: Male

Most application development data are in the form of key-value
data MySQL JSON XML class object member variables and the stored
data is also transmitted from the front end can be converted into the format Map ==> a key generation ==> class object ==> Push storage ==> database

Map interface <K, V>
- | class the HashMap <K, V> hash table structure
- | class TreeMap <K, V > underlying structure is a tree, a corresponding storage requirements K sort
Map bilateral queue keys (Key ) is unique, but the value (value) may be repeated

1.2 Map <K, V> Queue method Bilateral

By:
PUT (k K, V v);
into a key type, K and V must comply with generic constraints
putAll (Map <extends K, extends V??> Map);
into another queue bilateral Map and Map bilateral abutment require the addition of K and V and the current to be stored in Map
K and V consistent
deletion:
remove (Object K);
delete key K (key) value (value) of the corresponding
change:
PUT ( K k, V v);
corresponding to the current presence of K, modify the corresponding content
search:
int size ();
current Map bilateral queue, the effective number of key-value pairs
boolean isEmpty ();
if empty
boolean containsKey (Object key);
Analyzing Key specified whether there is a
boolean containsValue (Object value);
determining whether there Specifies value
set keySet ();
return queue all bilateral entire Map Key set corresponding set

[Note]
One way to use the end of the set, indicates that the method returns a collection type, mostly under the circumstances
is Set type
Collection values ();
returns the entire Map bilateral queue Collection collection of all Value corresponding
[Note]
method name if is a complex number, the type of the return value is an array where the majority set or

1.3 EntrySet

Entry of the key-value can be considered an object
defined within a Map
class Entry <K, V> {
K K;
V V;
}
K, V Map is entirely dependent on the constraints, here Entry is stored inside each key class object

Map provides a method
Set <Entry <K, V >> entrySet
return the key value is set to the class object Set
Set is a set of stored Entry type
Entry type is generic with

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Demo2 {
	public static void main(String[] args) {
		HashMap<String, String> map = new HashMap<String, String>();
		
		map.put("吴京", "谢楠");
		map.put("邓超", "娘娘");
		map.put("黄磊", "孙莉");
		map.put("王宝强", "XX");
		
		Set<Map.Entry<String, String>> entrySet = map.entrySet();
		
		System.out.println(entrySet);
	}
}

1.4 TreeMap <K, V> and Comparable and Comparator

K is the need for corresponding comparative way, if there is no way of comparison, can not be stored.

Comparator interface recommended

import java.util.Comparator;
import java.util.TreeMap;

public class Demo3 {
	public static void main(String[] args) {
		TreeMap<String,String> map = new TreeMap<String, String>();
		
		map.put("李四", "1");
		map.put("王五", "1");
		map.put("赵六", "1");
		map.put("张三", "1");
		
		System.out.println(map);
		
		TreeMap<Dog,String> map2 = new TreeMap<Dog, String>(new Comparator<Dog>() {

			@Override
			public int compare(Dog o1, Dog o2) {
				return o1.getAge() - o2.getAge();
			}
		});
		
		map2.put(new Dog("王可可", 1), "111"); 
		map2.put(new Dog("八公", 2), "111"); 
		map2.put(new Dog("豆豆", 3), "111"); 
		map2.put(new Dog("老黄", 4), "111"); 
		map2.put(new Dog("旺财", 5), "111"); 
		
		System.out.println(map2);
		
	}
}
Published 12 original articles · won praise 15 · views 3557

Guess you like

Origin blog.csdn.net/qq_42581682/article/details/104611643