Java SE 048 Map in-depth detailed explanation and two implementation methods of traversing Map

(1) As long as a person does not give up on himself, the whole world will not give up on you.
(2) I am born to be of great use . (3) If I
cannot bear the suffering of learning, I must bear the suffering of life. How painful it is Deep comprehension.
(4) You must gain from doing difficult things . (
5) Spirit is the real blade.
(6) Conquering opponents twice, the first time in the heart.
(7) Writing is really not easy. If you like it or have something for you Help remember to like + follow or favorite~

Java SE 048 Map in-depth detailed explanation and two implementation methods of traversing Map

1.Map (mapping)

Map a key to its value. A Map cannot contain duplicate keys. Each key can be mapped to one value, but not to multiple values.

As long as there is a map, we must think of a pair.
put(K key,V value);Associate the specified value to the associated key

## 2. Why
are the types returned by values () and keySet() different? Because there can be duplicate values ​​in the value set, but there can be no duplicate values ​​in the key set.
Set conforming key set cannot be repeated, and Collection conforming value set allows duplicate values.

package com.javase.map;

import java.util.HashMap;

public class MapTest2 {
    
    
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
    
    
		HashMap map = new HashMap();
		
		String str = "zhangsan";
		map.put("a", str);
		map.put("b", str);
		
		System.out.println(map);
	}
}

The keySet() method of Map will return a collection of keys, because the keys of Map cannot be repeated, so the return type of the keySet() method is Set; the value of Map can be repeated, so the return type of the values() method is Collection , Can accommodate repeated elements.

3. Traverse the map collection

package com.javase.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class MapTest3 {
    
    
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
    
    
		HashMap map = new HashMap();
		map.put("a", "aa");
		map.put("b", "bb");
		map.put("c", "cc");
		map.put("d", "dd");
		map.put("e", "ee");
		map.put("f", "ff");
		
		Set set = map.keySet();
		for(Iterator iter = set.iterator(); iter.hasNext();){
    
    
			String key = (String)iter.next();
			String value =(String)map.get(key);
			System.out.println(key+"="+value);
		}
	}
}

4. Pass in parameters through the command line, and count the number of times the same parameter appears in the command line parameter

package com.javase.map;

import java.util.HashMap;
import java.util.Iterator;

public class MapTest4 {
    
    
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
    
    
		HashMap map = new HashMap();
		
		for(int i = 0; i < args.length; i++){
    
    
			//如果不存在这个元素就返回null,则此时可将这个值放进map进行记录,不为null表明以前它已经存在过了
			//存在则可以将其取出来,然后再放回去即可
			if(map.get(args[i])==null){
    
    
				map.put(args[i], new Integer(1));
			}else{
    
    
				Integer in= (Integer)map.get(args[i]);
				in = new Integer(in.intValue()+1);
				map.put(args[i], in);
			}
		}
		
		for(Iterator iter = map.keySet().iterator();iter.hasNext();){
    
    
			String key = (String)iter.next();
			Integer value = (Integer)map.get(key);
			System.out.println(key+"出现"+value+"次");
		}
	}
}

Guess you like

Origin blog.csdn.net/xiogjie_67/article/details/108540813