HashMap<String, int[]> H = new HashMap<String, int[]>();
H.put("drdetroit", new int[]{1,2});
H.put("drdetroit", new int[]{1,3});
System.out.println(H.get("drdetroit").toString());
打印出来
[I@c3c749
我假设这是哈希值(是吗?)。我怎样才能让它打印我的实际值?
最佳答案
我相信这就是你所追求的。 由于 HashMap 只能存储唯一的键,因此如果您想要一 (1) 个键有多个值,则必须使用列表。
也许有更好的方法来实现这一目标,但它正在发挥作用,并且本身非常具有表现力,并且非常清楚它的作用和方式。
HashMap 现在接受 List 接口(interface),因此可以接受任何实现它的列表类型。这非常整洁! :)
ArrayList<int[]> values = new ArrayList<int[]>();
values.add(new int[]{1,2});
values.add(new int[]{1,3});
HashMap<String, List<int[]>> H = new HashMap<String, List<int[]>>();
H.put("drdetroit", values);
for(String key : H.keySet()) {
for(int[] array : H.get(key)){
System.out.println(Arrays.toString(array));
}
}
关于java - HashMap 违背了我的值(value)观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13720595/