[英]Eclipse Debugger: Variable view when using nested Java HashMap, ArrayList
EDIT2:好的,這最像Eclipse Debugger問題。 添加以下代碼給出了我的期望。 所以代碼沒有奇怪的行為,只有調試器讓我想到,有一個。 (Eclipse Indigo Build id 20110615-0604)
Object bla1 = map.get("String3");
Object bla2 = map.get("String4");
Object bla3 = map.get("String1");
Object bla4 = map.get("String2");
Object bla5 = map.get("innerMap");
Object bla6 = map.get("list");
編輯:這不是訂單問題! 我知道HashMap是什么,我不關心訂單。 問題是,put(“list”,list)不會創建新的HashMap條目,它會覆蓋舊條目。 put(“liste”,list)將添加一個新條目。 添加時,“列表”和“列表”條目都不存在。 請不要告訴我“你是新手”的答案,我沒有調試代碼示例(使用jdk1.6.0_24)。
我使用jdk1.6.0_24作為我的執行環境,遇到了一個對我來說非常奇怪的問題:
List<Object> list = new ArrayList<Object>();
list.add("A String 5");
list.add("A String 6");
Map<String, Object> aMap = new HashMap<String, Object>();
aMap.put("String7", "A String 7");
aMap.put("String8", "A String 8");
list.add(aMap);
Map<String, Object> innerMap = new HashMap<String, Object>();
innerMap.put("newString", "Yet another new String");
innerMap.put("bool", false);
innerMap.put("long", 3L);
innerMap.put("Integer", 3);
List<Object> anotherList = new ArrayList<Object>();
anotherList.add(1);
anotherList.add(1L);
anotherList.add("Another String");
anotherList.add(true);
List<Object> aList = new ArrayList<Object>();
aList.add(2);
aList.add(2L);
aList.add("Yet Another String");
aList.add(false);
anotherList.add(aList);
Map<String, Object> map = new HashMap<String, Object>();
map.put("String3", "A String 3");
map.put("String4", "A String 4");
map.put("String1", "A String 1");
map.put("String2", "A String 2");
map.put("innerMap", innerMap);
map.put("list", list); //here it happens
//map.put("list0", list); //here it happens also
//map.put("list1", list); //here it happens also
//map.put("list2", list); //this will work, when you use this line and comment out all 'here it happens' lines
map.put("anotherList", anotherList);
根據新條目的鍵值,對象列表(它是一個ArrayList)要么按照我的預期處理(添加到新位置上的映射),要么:列表將被添加到映射到其中一個先前位置的位置添加的對象是alredy坐着。 現有對象不會被覆蓋,可以通過next訪問。
誰能解釋一下,這里發生了什么? 你會期待這種行為嗎? 我還沒有嘗試過其他java版本。
HashMap
的鍵沒有可預測的順序。 如果要保留插入順序,請使用LinkedHashMap
,如果您想要按鍵排序(自然順序),則查找TreeMap
(兩者都是Collection API的一部分)
你在談論地圖的迭代順序嗎? 對於HashMap
,迭代順序沒有指定,並且可能是非常隨機的(當然它實際上並非如此,你只是乍一看不能輕易看到順序,因為它取決於對象的哈希值); 如果您需要保證訂單,請使用SortedMap
例如TreeMap
。
第一段的最后一句。
http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html
聽起來你是Java新手。 了解Javadoc並不斷使用它,它是你的朋友。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.