2018_3_26集合框架_4 Map接口-HashMap集合类

11.Map中存储的元素都由两个对象组成,既一个键对象和一个值对象,可以根据键实现对应值的映射

2.Map中的key不要求有序,不允许重复,value同样不要求有序,但允许重复

3.HashMap是最常用的实现类,他的存储方式是哈希表。优点:查询指定元素效率高

4.Map中常用的方法

5.实例:

public class Test11 {
	
	public static void main(String[] args) {
		//使用HashMap存储多组国家英文简称和中文全称的键-对值
		Map<K, V> countries =new HashMap();
		countries.put("CN", 中国);
		countries.put("RU",俄罗斯);
		countries.put("FR",法兰西);
		countries.put("US","美国");
		//显示”CN"对应国家的中文全称
		String country=(String)countries.get("CN");
		System.out.println("CN"+"是"+country);
		//显示集合中元素个数
		System.out.println(countries.size());
		//两次判断Map中是否存在“FR”键
		System.out.println("Map中包含FR的key吗?"+countries.containsKey("FR"));
		countries.remove("FR");
		System.out.println("Map中包含FR的key吗?"+countries.containsKey("FR"));
		//分别显示键集,值集,和键-值集
		System.out.println(countries.keySet());
		System.out.println(countries.values());
		System.out.prinltn(countries);
		//清空HashMap并判断
		countries.clear();
		if(countries.isEmpty)
			System.out.println("已经清空Map数据");
		
		
	}
}

2.

猜你喜欢

转载自blog.csdn.net/qq1043002305/article/details/79699058