简体   繁体   中英

Put Integer in a nested Hashmap in Java

I have a nested Hashmap in this structure: HashMap<Integer,HashMap<Integer, Integer>> . I'm trying to put data in this order:

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

The problem is that wrong user_id gets inserted into wrong item_id , thus giving wrong rating. I think there is a problem with the object reference of the inner HashMap, but I'm not sure.

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;

}

Thanks @JB Nizet . This works:

    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;

}

You should try to re-create a new innerHashMap on each iteration.

Actually you just have one so you mix the values in it.

Remove if condition

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

Since you need a new HashMap for each item id , So You can create new innerHashMap every time

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM