package com.edu;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Day09 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("001", "老万");
map.put("002", "i");
map.put("003", "yi");
map.put("004", "老");
map.remove("001"); //只能通过key去删除
map.isEmpty(); //返回boolean
map.size(); //返回int的长度
map.containsKey("001"); //返回boolean
map.isEmpty(); //返回boolean
// key值不容许重复
System.out.println(map);
Set<String> strings = map.keySet();//获取全部的key放入到set集合中去
System.out.println(strings);
Collection<String> values = map.values();//获取全部的value放入到collection中
System.out.println(values);
//ctrl+alt+v自动补全左边或者.var
Set<Map.Entry<String, String>>entries = map.entrySet();//获取key和value,等价于sout(map)
System.out.println(entries);
System.out.println("============");
for (Map.Entry<String, String> a: entries){//增强for来分别获取key和value
System.out.println(a.getKey());
System.out.println(a.getValue());
}
}
}
Map中常用的方法
于 2023-10-28 23:16:36 首次发布