大家好,又见面了,我是你们的朋友全栈君。
COPY/** * Map接口的使用 * 特点:1.存储键值对 2.键不能重复,值可以重复 3.无序 */
public class Demo1 {
public static void main(String[] args) {
Map<String,Integer> map=new HashMap<String, Integer>();
//1.添加元素
map.put("tang", 21);
map.put("he", 22);
map.put("fan", 23);
System.out.println(map.toString());
//2.删除元素
map.remove("he");
System.out.println(map.toString());
//3.遍历
//3.1 使用keySet();
for (String key : map.keySet()) {
System.out.println(key+" "+map.get(key));
}
//3.2 使用entrySet();效率较高
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/139203.html原文链接:https://javaforall.net