package mapDemp;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class mapTest {
public static void main(String[] args) {
Map map = new HashMap<>();
map.put("heima001", "小一");
map.put("heima002", "小二");
map.put("heima003", "小三");
map.put("heima004", "小四");
map.put("heima005", "小五");
// 移除
String heima001 = map.remove("heima001");
System.out.println(heima001);
System.out.println(map);
// 判断集合中是否包含指定的键
boolean heima002 = map.containsKey("heima002");
System.out.println(heima002);
// 判断集合中是否包含指定的值
boolean mc = map.containsValue("小六");
System.out.println(mc);
// 判断集合是否为空
boolean empty = map.isEmpty();
System.out.println(empty);
// 清空集合
map.clear();
System.out.println(map);
// 返回集合长度
int size = map.size();
System.out.println(size);
// 使用keySet获得所有的键,
// 再通过增强for循环遍历Set集合得到每一个键,
// 最后通过每一个键获得对应的值
Set keys = map.keySet();
for (String key : keys) {
String value = map.get(key);
System.out.println(key+"-----"+value);
}
// 首先获得所有的键值对对象
// Set集合中装的是键值对对象(Entry对象)
// Entry里面装的是键和值
Set> en = map.entrySet();
// 通过增强for循环遍历
for (Map.Entry stringStringEntry : en) {
// 使用get方法获得对象中的键和值
String key = stringStringEntry.getKey();
String value = stringStringEntry.getValue();
System.out.println(key+"------"+value);
}
}
}