Hashmap遍历查询问题

编程入门 行业动态 更新时间:2024-10-26 22:19:39
Hashmap遍历查询问题 我有一个Hashmap,里面的数据如下: (“a”,1) (“a”,100) (“a”,2) (“b”,3) (“b”,44) (“b”,5555) (“c”,1111) (“c”,9) 我想把key为a的便利出来,存入到其他容器里。如果key “a” , "b" ,“c”是常量好遍历,但是key是变量,这个我还没想到好的方法,求高人指点!!!

最满意答案

首先,map中是不能存key 相同的值,如果key相同,则key对应的value为最后一次存的值; 然后,遍历map Map root=new HashMap(); root.put("a", 1); root.put("a", 100); root.put("a", 2); root.put("b", 3); root.put("b", 44); root.put("b", 5555); root.put("c", 1111); root.put("c", 9); Iterator it=root.entrySet().iterator(); while(it.hasNext()){ Map.Entry entry = (Entry ) it.next(); System.out.println(entry.getKey()); System.out.println(entry.getValue()); } 最后,按照你这种想法,可以将hashmap 换成 arrayList;

更多推荐

本文发布于:2023-07-28 16:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1306848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:遍历   Hashmap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!