HashMap,LinkedHashMap取值特点

本文对比了HashMap与LinkedHashMap在取值顺序上的不同特点。HashMap不保证元素的迭代顺序,而LinkedHashMap则按插入顺序返回元素。通过示例代码演示了两种Map实现类在遍历输出时的行为差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)HashMap取值--->不是按照插入顺序

HashMap<String,Integer> hm=new HashMap<String, Integer>();
hm.put("大学语文",3);
hm.put("英语",1);
hm.put("音乐鉴赏",5);
hm.put("数学",2);
hm.put("形式政策",4);
for (Map.Entry<String ,Integer> entry:hm.entrySet()){
    System.out.println(entry.getKey()+" : "+entry.getValue());
}

结果:

(二):LinkedHashMap的取值--->是按照插入取值

LinkedHashMap<String,Integer> lhm=new LinkedHashMap<String,Integer>();
lhm.put("化学",1);
lhm.put("生物",2);
lhm.put("物理",3);
lhm.put("语文",4);
lhm.entrySet();
for (Map.Entry<String,Integer> entry:lhm.entrySet()){
    System.out.println(entry.getKey()+" : "+entry.getValue());
}

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值