赞
赏
使用 Map 集合,存储键值对数据,然后进行增删改查的操作。
请定义一个 HashMap 集合,存储键值对数据,然后对集合元素进行增删改查的操作。
public class TestMap {
public static void main(String[] args) {
System.out.println("嗨客网(www.haicoder.net)\n");
HashMap<String,Integer> map = new HashMap<>();
// 添加元素
map.put("小米",1111);
map.put("华为",4999);
map.put("苹果",6999);
System.out.println(map);
// 修改元素
map.put("小米",3999);
System.out.println(map);
// 删除元素
map.remove("小米");
System.out.println(map);
// 查询元素
Integer i = map.get("华为");
System.out.println(i);
// 是否包含
Boolean b = map.containsKey("苹果");
System.out.println(b);
}
}
运行结果如下图:
以上案例存储键值对数据到 HashMap 集合中,然后对集合元素进行增删改查操作。