JAVA------Map of Collection

Overview of Map Collection

  • Interface Map<K,V>K: Type of key; V: Type of value
  • The object that maps keys to values; cannot contain duplicate keys; each key can be mapped to at most one value Example: student ID and name
  • Create the objects of the Map collection
    1. Polymorphic way
    2. Specific implementation class HashMap

Look at a piece of code:

package Map;

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

public class MapDemo01 {
    
    

	public static void main(String[] args) {
    
    
		Map<String,String> map=new HashMap<>();
		
		//V put(k key,V value) 将指定的值与该映射中的指定键相关联
		map.put("lll", "小李");
		map.put("www", "小王");
		map.put("zzz", "小张");
		map.put("zzz", "小赵");//键重复的时候,值就会把以前的值替换掉
		
		System.out.println(map);//{www=小王, zzz=小赵, lll=小李}
	}
}


Basic functions of Map collection

  • V put(K key,V value): add element
  • V remove(Object key): Delete key-value pair elements according to the key
  • void clear(): Remove all key-value pair elements
  • boolean containsKey(Object key): Determine whether the collection contains the specified key
  • boolean containsValue(Object value): Determine whether the collection contains the specified value
  • boolean isEmpty(): Determine whether the collection is empty
  • int size(): The length of the collection, which is the number of key-value pairs in the collection

Look directly at the code demo:

package Map;

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

public class MapDemo02 {
    
    
	
	public static void main(String[] args) {
    
    
		Map<String,String> map=new HashMap<>();
		
		//V put(k key,V value) 将指定的值与该映射中的指定键相关联
		map.put("lll", "小李");
		map.put("www", "小王");
		map.put("zzz", "小张");
				
		//System.out.println(map.remove("lll"));
		//map.clear();
		
		System.out.println(map.containsKey("lll"));
		System.out.println(map.containsKey("lll0"));
		
		System.out.println(map.isEmpty());
		System.out.println(map.size());
		
		System.out.println(map);
	}
}


Get function of Map collection

  • V get(Object key): Get the value according to the key
  • Set keySet(): Get the set of all keys, the key is unique, return the Set set
  • Collection values(): Get a collection of all values, the value is not necessarily unique, return the Collection collection
  • Set<Map.Entry<K,V>> entrySet(): Get the collection of all key-value pair objects
    1.getKey() get the key
    2.getValue get the value

Look directly at the code demo:

package Map;

import java.util.Collection;

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

public class MapDemo03 {
    
    

	public static void main(String[] args) {
    
    
		Map<String,String> map=new HashMap<>();
		
		//V put(k key,V value) 将指定的值与该映射中的指定键相关联
		map.put("lll", "小李");
		map.put("www", "小王");
		map.put("zzz", "小张");

		System.out.println(map.get("lll"));//小李
		System.out.println(map.get("llll"));//null
		
		Set<String> set=map.keySet();
		for(String s:set) {
    
    
			System.out.print(s+" ");
		}
		System.out.println();
		
		Collection<String> col=map.values();
		for(String c:col) {
    
    
			System.out.print(c+" ");
		}
		System.out.println();
		
		//遍历
		Set<Map.Entry<String, String>> entry=map.entrySet(); 
		for(Map.Entry<String, String> me:entry) {
    
    
			//根据键值对对象获取键和值
			String key=me.getKey();
			String value=me.getValue();
			System.out.println(key+":"+value);
		}
		
	}
}

Guess you like

Origin blog.csdn.net/weixin_45102820/article/details/113561423