2015-04-03 51 views
0

我在此結構中具有嵌套Hashmap:HashMap<Integer,HashMap<Integer, Integer>>。我試圖把數據按以下順序:將整數放入Java中的嵌套Hashmap中

{ 
item_id1 -> { {user_id1 -> rating} , {user_id2 -> rating} ...} 
item_id2 -> ... 
... 
} 

的問題是錯誤的user_id被插入錯誤的item_id,從而使錯誤的評級。我認爲內部HashMap的對象引用有問題,但我不確定。

public HashMap<Integer, HashMap<Integer, Integer>> getUserRating() throws SQLException { 

    HashMap<Integer,HashMap<Integer, Integer>> ratings = 
      new HashMap<Integer,HashMap<Integer, Integer>>(); 

    String query = "SELECT * FROM rating"; 
    resultSet = statement.executeQuery(query); 

    Integer rating; 
    Integer user; 
    Integer item; 
    HashMap<Integer,Integer> innerHashMap = null; 
    while(resultSet.next()) { 
     item = resultSet.getInt(2); 
     user = resultSet.getInt(1); 
     rating = resultSet.getInt(3); 

     if(innerHashMap == null) { 
      innerHashMap = new HashMap<Integer,Integer>(); 
     } 
     innerHashMap.put(user, rating); 
     ratings.put(item,innerHashMap); 

    } 

    // item_id = 400 , user_id = 44490. Should print rating = 4, but prints 2 
    System.out.println(ratings.get(400).get(44490)); 

    return ratings; 

} 
+0

步調與調試器。 – Kon 2015-04-03 14:53:01

+1

提示:你調用了多少次'innerHashMap = new HashMap ();'?你是否每個項目調用一次? – 2015-04-03 14:55:31

+0

@JB Nizet好的,我明白你的觀點。假設item_id是有序的,我現在只創建一個新的內部HashMap實例,如果有一個新的項目。但是現在我在'innerHashMap.put(user,rating);' – user2097519 2015-04-03 15:04:24

回答

0

您應該嘗試在每次迭代中重新創建一個新的innerHashMap。

其實你只有一個,所以你混合它的值。

-1

刪除if條件

if(innerHashMap == null) { 
    innerHashMap = new HashMap<Integer,Integer>(); 
} 

因爲你需要爲每個項目ID的新的HashMap, 這樣你就可以創建新的innerHashMap每次

while(resultSet.next()) { 
    item = resultSet.getInt(2); 
    user = resultSet.getInt(1); 
    rating = resultSet.getInt(3); 
    innerHashMap = new HashMap<Integer,Integer>(); <--- New Instance every time 
    innerHashMap.put(user, rating); 
    ratings.put(item,innerHashMap); 
} 
+0

這是不正確的,我試過了,我只用這個解決方案每item_id得到1 user_id。 – user2097519 2015-04-03 15:13:57

+0

你只會得到一個單一的,因爲在每次數據庫迭代你的物品ID正在改變! – 2015-04-03 15:16:18

+0

如果你需要1個項目ID,多個地圖項,那麼你應該找出所有的用戶ID與評級單個項目ID,然後添加! – 2015-04-03 15:17:42

1

感謝@JB Nizet。這工作:

public HashMap<Integer, HashMap<Integer, Integer>> getUserRating() throws SQLException { 
    HashMap<Integer,HashMap<Integer, Integer>> ratings = 
      new HashMap<Integer,HashMap<Integer, Integer>>(); 

    String query = "SELECT * FROM rating"; 
    resultSet = statement.executeQuery(query); 

    Integer rating; 
    Integer user; 
    Integer item; 

    HashMap<Integer,Integer> innerHashMap = null; 
    while(resultSet.next()) { 
     item = resultSet.getInt(2); 
     user = resultSet.getInt(1); 
     rating = resultSet.getInt(3); 

     if(ratings.get(item) == null) { 
      innerHashMap = new HashMap<Integer,Integer>(); 
     } 

     innerHashMap.put(user, rating); 
     ratings.put(item,innerHashMap); 


    } 

    // item_id = 400 , user_id = 44490. Should print rating = 4 
    System.out.println(ratings.get(400).get(44490)); 

    return ratings; 

} 
+1

這仍然不正確。事實上,如果ratings.get(item)不等於null,那麼您將爲與前一項關聯的內部HashMap添加一個評級。它應該是'innerHashMap = ratings.get(item); if(innerHashMap == null){innerHashMap = new HashMap <>(); }' – 2015-04-03 16:11:32