初认Map

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qwer1203355251/article/details/52939171
import java.util.HashMap;
import java.util.Map;

public class Map1
{
	public static void main(String[] args) 
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put("aa","AA");//put返回值为值的类型
		map.put("bb","BB");//前键  后值  键不能重复 值可以重复
		map.put("ee","BB");
		map.put("cc","CC");
		map.put("dd","DD");
		System.out.println("put return  "+map.put("aa","A"));
		System.out.println(map);//HashMap无序
		//put的返回值为  AA,aa键的值  A把AA 赶跑了
//		Map<String, String> map2 = new HashMap<String, String>();
//		map2.put("ee","EE");
//		map2.put("ff","FF");
		System.out.println("remove return   "+map.remove("ee"));//根据键删除一条map中的数据,返回的是该键对应的值
		System.out.println(map);
		//map.clear();
		System.out.println("clear没有返回值,此时map中为:"+map);
		System.out.println("判断map中是否包含指定的键 ee   "+map.containsKey("aa"));
		System.out.println("判断map中是否包含指定的值    "+map.containsValue("a"));
		System.out.println("空吗  "+map.isEmpty());
		map.clear();
		map.put(null, null);
		System.out.println("空吗  "+map.isEmpty());
		System.out.println(map);
	}
}
 


猜你喜欢

转载自blog.csdn.net/qwer1203355251/article/details/52939171
今日推荐