四个常用方法 put remove get containsKey
public V put (K key, V value)把指定的键和指定的值放入map集合中
返回值v
存储键值对的时候,key不重复,返回值V是null
存储键值对的时候,key重复,会使用新的value替换map中重复的value 返回被替换的value的值
public V remove(Object key)把指定的键所对应的键值对元素在map里删除返回被删除的键值对的值
key存在,v返回被删除的值
key不存在,v返回null
数字建议尽量用Integer.int遇到null会报错
public V get(Object key)根据指定的键,在map集合中获取对应的值
key存在,v返回对应的值
key不存在,v返回null
public boolean containsKey(Object key)判断map集合中是否包含指定的值
包含返回true 不包含返回false
package xuexi.heima.map;
import java.util.HashMap;
import java.util.Map;
public class demo01 {
public static void main(String[] args) {
show1();
System.out.println("--------");
show2();
System.out.println("-----------");
show3();
System.out.println("--------");
show4();
}
private static void show4() {
Map map4 = new HashMap<>();
map4.put("哈哈",12);
map4.put("洗洗",12);
map4.put("呵呵",12);
map4.put("试试",12);
System.out.println(map4);
boolean v1 = map4.containsKey("哈哈");
System.out.println(v1);
boolean v2 = map4.containsKey("xixi");
System.out.println(v2);
}
private static void show3() {
Map map3 = new HashMap<>();
map3.put("哈哈",12);
map3.put("洗洗",12);
map3.put("呵呵",12);
map3.put("试试",12);
System.out.println(map3);
Integer s1 = map3.get("哈哈");
System.out.println(s1);
}
private static void show2() {
Map map2 = new HashMap<>();
map2.put("哈哈",12);
map2.put("吼吼",132);
map2.put("嘘嘘",11);
map2.put("讷讷",14);
System.out.println(map2);
Integer re = map2.remove("哈哈");
System.out.println(re);
System.out.println(map2);
}
private static void show1() {
Map map1 = new HashMap<>();
String v1 = map1.put("李晨", "范冰冰");
String v2 = map1.put("李晨", "范冰冰2");
System.out.println(v1);
System.out.println(v2);
System.out.println(map1);
System.out.println("==========");
map1.put("1","2");
map1.put("2","3");
map1.put("3","3");
System.out.println(map1);
}
}
分享到: