Map集合的常用方法

Map集合的常用方法

	Object put(Object key, Object value)返回值是被替换的值。
	Object remove(Object key)	//删除该Map中key所对应的键值对
	void putAll(Map mapping)	//在Map后面追加新的键值对,如果Map中已包含新Map中的key则将key所对应的value用新value覆盖
	void clear()		//清空Map中的键值对,使其为空
	Object get(Object key)	//根据key获取对应value
	boolean containsKey(Object key)	//判断该Map中是否包含指定key,包含则返回true,否则返回false
	boolean containsValue(Object value)	//判断Map中是否包含指定value,包含则返回true,否则返回false
	int size()	//返回该Map的大小
	boolean isEmpty()	//判断该Map是否为空,如果为空则返回true,否则返回false。
	public Set keySet()	
	public Collection values()
	public Set entrySet()
	
	
	public class MapTest {

	public static void main(String[] args) {
			//put();
			//remove();
			//putAll();
			//clear();
			//get();
			//containsKey();
			//containsValue();
			//size();
			//isEmpty();
			// keySet();
			//values();
			//entrySet();
	}
			
	//返回键值对
	public static void entrySet() {
		Map strMap = new HashMap();
		strMap.put("a", "aaa");
		strMap.put("b", "bbb");
		strMap.put("c", "ccc");
		strMap.put("d", "ddd");
		strMap.put("e", "eee");
		strMap.put("f", "fff");
		Set> strSet = strMap.entrySet();
		for (Entry entry : strSet) {
			System.out.println(entry);
		}
	}
	
	//返回值的集合
	public static void values() {
		Map strMap = new HashMap();
		strMap.put("a", "aaa");
		strMap.put("b", "bbb");
		strMap.put("c", "ccc");
		strMap.put("d", "ddd");
		strMap.put("e", "eee");
		strMap.put("f", "fff");
		Collection values=strMap.values();
		for (String value : values) {
			System.out.println(value);
		}
	}
	
	//返回键的集合
	public static void keySet() {
		Map strMap = new HashMap();
		strMap.put("a", "aaa");
		strMap.put("b", "bbb");
		strMap.put("c", "ccc");
		strMap.put("d", "ddd");
		strMap.put("e", "eee");
		strMap.put("f", "fff");
		Set strSet=strMap.keySet();
		for (String str : strSet) {
			System.out.print(str+"\t");
		}
	}
	
	//判断该Map是否为空,如果为空则返回true,否则返回false。
	public static void isEmpty() {
		Map strMap = new HashMap();
		strMap.put("a", "aaa");
		strMap.put("b", "bbb");
		strMap.put("c", "ccc");
		strMap.put("d", "ddd");
		strMap.put("e", "eee");
		strMap.put("f", "fff");
		Map strMap01 = new HashMap();
		boolean flag = strMap.isEmpty();
		boolean flag01=strMap01.isEmpty();
		System.out.println(flag+"\t"+flag01);
	}
	
	//返回该Map的大小
	public static void size() {
		Map strMap = new HashMap();
		strMap.put("a", "aaa");
		strMap.put("b", "bbb");
		strMap.put("c", "ccc");
		strMap.put("d", "ddd");
		strMap.put("e", "eee");
		strMap.put("f", "fff");
		int mapSize = strMap.size();
		System.out.println("该Map大小为:"+mapSize);
	}
	
	//判断该Map中是否包含所指定value,若包含则返回true,否则返回false
	public static void containsValue() {
		Map strMap = new HashMap();
		strMap.put("a", "aaa");
		strMap.put("b", "bbb");
		strMap.put("c", "ccc");
		strMap.put("d", "ddd");
		strMap.put("e", "eee");
		strMap.put("f", "fff");
		boolean flag = strMap.containsValue("aaa");
		boolean flag01=strMap.containsValue("qqq");
		System.out.println(flag+"\t"+flag01);
	} 
	
	//判断该Map中是否包含指定key,如果包含则返回true,否则返回false。
	public static void containsKey() {
		Map strMap = new HashMap();
		strMap.put("a", "aaa");
		strMap.put("b", "bbb");
		strMap.put("c", "ccc");
		strMap.put("d", "ddd");
		strMap.put("e", "eee");
		strMap.put("f", "fff");
		boolean flag = strMap.containsKey("a");
		boolean flag01=strMap.containsKey("q");
		System.out.println(flag+"\t"+flag01);
	}
	
	//获取key所对应的value
	public static void get() {
		Map strMap = new HashMap();
		strMap.put("a", "aaa");
		strMap.put("b", "bbb");
		strMap.put("c", "ccc");
		strMap.put("d", "ddd");
		strMap.put("e", "eee");
		strMap.put("f", "fff");
		String value=strMap.get("a");
		System.out.println(value);
	}
	
	//清空Map中的键值对,使其为空
	public static void clear() {
		Map strMap = new HashMap();
		strMap.put("a", "aaa");
		strMap.put("b", "bbb");
		strMap.put("c", "ccc");
		strMap.put("d", "ddd");
		strMap.put("e", "eee");
		strMap.put("f", "fff");
		for (Map.Entry keyValue : strMap.entrySet()) {
			System.out.println(keyValue);
		}
		strMap.clear();
		System.out.println("执行该方法后:");
		for (Map.Entry keyValue : strMap.entrySet()) {
			System.out.println(keyValue);
		}
		
	}
	
	//将指定Map追加到Map中,如果所包含key已存在,则所对应的value将被覆盖
	public static void putAll() {
		Map strMap = new HashMap();
		strMap.put("a", "aaa");
		strMap.put("b", "bbb");
		strMap.put("c", "ccc");
		strMap.put("d", "ddd");
		strMap.put("e", "eee");
		strMap.put("f", "fff");
		Map strMap01 = new HashMap();
		strMap.put("a", "abc");
		strMap.put("c", "asx");
		strMap.put("h", "hhh");
		for (Map.Entry keyValue : strMap.entrySet()) {
			System.out.println(keyValue);
		}
		strMap.putAll(strMap01);
		for (Map.Entry keyValue : strMap.entrySet()) {
			System.out.println(keyValue);
		}
		
	}
	
	//根据键从Map中删除对应键值对
	public static void remove() {
		Map strMap = new HashMap();
		strMap.put("a", "aaa");
		strMap.put("b", "bbb");
		strMap.put("c", "ccc");
		strMap.put("d", "ddd");
		strMap.put("e", "eee");
		strMap.put("f", "fff");
		for (Map.Entry keyValue : strMap.entrySet()) {
			System.out.println(keyValue);
		}
		strMap.remove("a");
		for (Map.Entry keyValue : strMap.entrySet()) {
			System.out.println(keyValue);
		}
	}
	
	//新增一个键值对,如果该键已存在,则新的值会覆盖旧的值。
	public static void put() {
		Map strMap = new HashMap();
		strMap.put("a", "aaa");
		strMap.put("b", "bbb");
		strMap.put("c", "ccc");
		strMap.put("d", "ddd");
		strMap.put("e", "eee");
		strMap.put("f", "fff");
		for (Map.Entry keyValue : strMap.entrySet()) {
			System.out.println(keyValue);
		}
		strMap.put("a", "abc");
		for(Map.Entry keyValue : strMap.entrySet()) {
			System.out.println(keyValue);
		}
	}
	

}

你可能感兴趣的:(基础知识)