Map collection (note 21)

Map collection

1. Map collection

The Map set provides a mapping from key to value. The Map cannot contain the same key value, and each key can only map to the same value. The key value also determines the storage location of the storage object in the map. The Map set includes the Map interface And the classes implemented by the Map interface.
statement 
public interface Map<K,V> 
The map collection is unordered, there can be no duplicate keys, and possibly duplicate values
=>HashMap     public class HashMap<K,V>extends AbstractMap<K,V>implements Map<K,V>, Cloneable, Serializable
=>HashTable  public class Hashtable<K,V>extends Dictionary<K,V>implements Map<K,V>, Cloneable, Serializable
->TreeMap bottom derivative is a binary sorting tree
Difference between HashMap and HashTable
1) Both HashMap and HashTable are subclasses of Map (because they both implement the Map interface), but HashTable also inherits the Dictionary class
2) HashMap can have null values ​​and null keys, HashTable cannot
3) HashTable is relatively old since jdk1.0, it is thread-safe, but Map is not thread-safe

Operations on Map Collections
V put(K key, V value)  
void putAll(Map<? extends K,? extends V> m)  
V remove(Object key)
int size()    
V get(Object key)        
boolean containsKey(Object key) 
boolean containsValue(Object value)
Set<K> keySet() 
boolean equals(Object o)  
Collection<V> values()    
boolean isEmpty() 
void clear()  
Set<Map.Entry<K,V> entrySet()  
V put(K key, V value) //Put data into the map collection void putAll(Map<? extends K,? extends
V> m) V remove(Object key) //According to the key, remove an element int size( ) //Check how many groups of elements there are in the set V
get(Object key) //According to the key to take the value 
boolean containsKey(Object key) to judge whether it contains a key 
boolean containsValue(Object value) //Determine whether it contains a value
Set <K> keySet() //Get all keys and return boolean in Set equals(Object o)
Collection<V> values() //Get all values ​​and return 
boolean in Collection isEmpty() //Judging whether it is empty or not void clear() //Clear
Set<Map.Entry<K,V> entrySet()

 
//Example 1, the simplest example
static void demo(){
Map map=new HashMap();
map.put("01", "War Criminal No. 1"); 
map.put("02", "War Criminal No. 2"); 
map.put("03", "Three War Criminals"); 
map.put("04", "War Criminal No. 4"); 
System.out.println(map.size()); //4
System.out.println(map); //{04=War criminal No.4, 01=War criminal No.1, 02=War criminal No.2, 03=War criminal No.3} Disorder
}

// Example 2 
static void demo2() {
Map<String, String> map = new HashMap<String, String>();
map.put("Food 1", "Chicken");
map.put("Food 2", "Dog meat");
map.put("Food 3", "Heart-protecting meat");
map.put("Food 4", "Rabbit");
map.put("Food Four", "What kind of meat is this"); // If the key is the same, it will be overwritten
map.put(null, "This is the value corresponding to null");
map.put("Food Five", "Durian");
map.put("Food Six", null);
System.out.println(map);
System.out.println(map.size());
System.out.println(map.get("Food Five")); // Durian
System.out.println(map.containsKey("美食四"));// true
System.out.println(map.containsValue("狗肉"));// true
map.remove("Food Four");
System.out.println(map);
// map.clear();
// System.out.println(map.isEmpty()); //true
// get all keys
Set<String> keyset = map.keySet();
Iterator<String> it = keyset.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
// get all values
Collection<String> c = map.values();
Iterator<String> it2 = c.iterator();
while (it2.hasNext()) {
System.out.println(it2.next());
}
//The return value of the map set put method
String result=map.put("Food 5", "Banana"); //Use banana to replace durian
System.out.println(result); //Durian
}

Second, the traversal of the Map collection

static void demo3(){
Map<String, String> map = new HashMap<String, String>();
map.put("Food 1", "Chicken");
map.put("Food 2", "Dog meat");
map.put("Food 3", "Heart-protecting meat");
map.put("Food 4", "Rabbit");
map.put("Food Four", "What kind of meat is this"); // If the key is the same, it will be overwritten
map.put(null, "This is the value corresponding to null");
map.put("Food Five", "Durian");
map.put("Food Six", null);

//Method 1, first take all the keys, then loop, take the value corresponding to the key one by one
Set <String >keyset= map.keySet();
Iterator<String> it=keyset.iterator();
while(it.hasNext()){
String key=it.next();
String value=map.get(key);
System.out.println(key+":"+value);
}

//Method 2, use Map.Entry type
Set<Map.Entry<String, String>> entrySet= map.entrySet();
Iterator<Map.Entry<String, String>> it2= entrySet.iterator();
while(it2.hasNext()){
Map.Entry<String, String> item =it2.next();
System.out.println(item.getKey()+":::"+item.getValue());
}
}

3. Store custom objects in the Map collection

Requirements, define a class of prisoners (name, ID number, crime), and store several class objects in the Map collection
Traverse and call the methods provided by this class
public class Test {
public static void main(String[] args) {
// List<int> list=new ArrayList<int>(); cannot use primitive data types to specify generics
// List<Integer> list=new ArrayList<Integer>(); 可以
Japan p1=new Japan("001","Abe Shinzo","Not a human being a dog");
Japan p2=new Japan("002","Obama","Dog");
Japan p3=new Japan("003","Chen Shuibian","Being a pug");
Map<String,Japan> map=new HashMap<String,Japan>();
map.put(p1.idCard, p1);
map.put(p2.idCard, p1);
map.put(p3.idCard, p1);
//Map.Entry<K, V>
Set <Map.Entry<String, Japan>>entrySet= map.entrySet(); 
Iterator<Map.Entry<String, Japan>> it=entrySet.iterator();
while(it.hasNext()){
Map.Entry<String, Japan> item=it.next();
String key=item.getKey();
Japan japan =item.getValue();
System.out.println("编号:"+key);
System.out.println("Drag out and kill");
japan.kill();
//System.out.println(item.getKey()+":"+item.getValue());
}
}
}

class Japan{
public Japan(String idCard, String name, String crime) {
this.idCard = idCard;
this.name = name;
this.crime = crime;
}
public String idCard;
public String name;
public String crime;
void kill(){
System.out.println("I am a criminal:"+this.name+ "My crime is:"+this.crime);
System.out.println("啊....im game over... ");
}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325565046&siteId=291194637