修改与Java HashMap中给定键关联的值
首先,创建一个HashMap并向其中添加元素-
//创建一个哈希映射
HashMap hm = new HashMap();
//将元素放入映射
hm.put("Bag", new Integer(1100));
hm.put("Sunglasses", new Integer(2000));
hm.put("Frames", new Integer(800));
hm.put("Wallet", new Integer(700));
hm.put("Belt", new Integer(600));现在,要修改与给定键关联的值,请使用put()方法。在这里,我们正在修改键“Frames”的值-
hm.put("Frames", "900");以下是修改与给定键关联的值的示例-
示例
import java.util.*;
public class Demo {
public static void main(String args[]) {
//创建一个哈希映射
HashMap hm = new HashMap();
//将元素放入映射
hm.put("Bag", new Integer(1100));
hm.put("Sunglasses", new Integer(2000));
hm.put("Frames", new Integer(800));
hm.put("Wallet", new Integer(700));
hm.put("Belt", new Integer(600));
//获取一组条目
Set set = hm.entrySet();
System.out.println("Elements in HashMap...");
System.out.println(set);
hm.put("Frames", "900");
System.out.println("\nUpdated HashMap Elements...");
System.out.println(hm);
}
}输出结果
Elements in HashMap...
[Frames=800, Belt=600, Wallet=700, Bag=1100, Sunglasses=2000]
Updated HashMap Elements...
{Frames=900, Belt=600, Wallet=700, Bag=1100, Sunglasses=2000}热门推荐
1 甄嬛传祝福语简短
9 薛之谦祝福语简短
10 叶海燕老师祝福语简短
11 祝福语女友文案简短霸气
12 生日祝福语简短搞笑女儿
13 新年祝福语简短创意兄弟
14 阳历新年祝福语大全 简短
15 结婚祝福语亲姐姐简短
16 双节祝福语简短英文
17 哥哥祝福语简短霸气句子
18 新娘对伴娘祝福语简短