import java.util.*;
/*
-
Map集合的常用方法:
-
---put()方法:添加指定的key值,和制定的value值到Map集合中。
-
---remove方法:把指定键,所对应的键值对元素,在Map集合中删除;返回被删除元素的值。
-
---get()方法:获取指定键,在Map集合中获取对应的值。
-
---keySet()方法:获取Map集合中指定的键,存储到Map集合中。
-
---containKey()方法:判断该集合中是否有此键。
-
*/
public class Demo03_Day04_Map集合的常用方法 {public static void main(String[] args) {
//创建一个Map集合的对象。
Map<Integer,String> maps=new HashMap<>();
//添加元素。
maps.put(1,“蒋干”);
maps.put(2,“刘备”);
maps.put(3,“关羽”);
maps.put(1,“曹操”);
maps.put(4,“张飞”);
maps.put(2,“蒋干”);
System.out.println(maps);
//删除元素。
maps.remove(1);
System.out.println(maps);
//创建Set集合,获取Map集合中所有的键值。
Set set=new HashSet<>();
set=maps.keySet();
//提取Map集合中,所有的值到Collection中.
Collection coll=maps.values();//判断集合中是否含有某个键。如果包含返回true,否则,返回false. System.out.println(maps.containsKey(2)); System.out.println(maps.containsKey(12)); //清空集合。 maps.clear(); System.out.println(maps); //获取集合的个数。 System.out.println(maps.size());
}
}