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是变量,这个我还没想到好的方法,求高人指点!!!
entry = (Entry
) it.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
最后,按照你这种想法,可以将hashmap 换成
arrayList;
最满意答案
首先,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更多推荐
发布评论