tnaser
tnaser

Reputation: 181

Initialize data in TreeMap

Given data such as : user, library ID, book ID, year

1,10, 100,2000
2,10, 100,2000
3,10, 100,2000

2,10, 200,2000
3,10, 200,2000
1,10, 200,2000

I tried storing them using TreeMap but the second group overwrite the first , any suggestions ?

    TreeMap<Integer, Integer> bookMap = new TreeMap<Integer, Integer>();
    TreeMap<Integer, TreeMap<Integer, Integer>> libraryMap = new TreeMap<Integer, TreeMap<Integer, Integer>>();
    TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, Integer>>> UserMap = new TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, Integer>>>();

    bookMap.put(100, 2000);
    libraryMap.put(10,bookMap);     
    UserMap.put(1,libraryMap);
    UserMap.put(2,libraryMap);
    UserMap.put(3,libraryMap);

    bookMap = new TreeMap<Integer, Integer>(); 
    bookMap.put(200, 2000);
    libraryMap = new TreeMap<Integer, TreeMap<Integer, Integer>>();
    libraryMap.put(10,bookMap);
    UserMap.put(1,libraryMap);
    UserMap.put(2,libraryMap);
    UserMap.put(3,libraryMap);

Solution #1 still using maps only, but I think this solution will take long time and consume more memory ?

TreeMap<Integer, Integer> bookMap = new TreeMap<Integer, Integer>();
        TreeMap<Integer, TreeMap<Integer, Integer>> libraryMap = new TreeMap<Integer, TreeMap<Integer, Integer>>();
        TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, Integer>>> UserMap = new TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, Integer>>>();

        bookMap.put(100, 2000);
        bookMap.put(300, 2003);
        libraryMap.put(10,bookMap);     
        UserMap.put(1,libraryMap);
        UserMap.put(2,libraryMap);
        UserMap.put(3,libraryMap);

        int userID = 1;
        int libraryID =10;
        if (UserMap.containsKey(userID))
        {           
             if ( UserMap.get(userID).containsKey(libraryID))
             {
                 TreeMap<Integer, Integer> storedBookMap = UserMap.get(userID).get(libraryID);
                 bookMap= new TreeMap<Integer, Integer>();

                 bookMap.putAll(storedBookMap);
                 bookMap.put(200, 2000);

                 libraryMap = new TreeMap<Integer, TreeMap<Integer, Integer>>();
                 libraryMap.put(libraryID,bookMap); 

                 UserMap.put(userID,libraryMap);
             }
        }

Upvotes: 1

Views: 19118

Answers (2)

James
James

Reputation: 8586

If you have the luxury of using arbitrary code, consider using one of the Multimap collections, which handle the problem you're trying to solve much more nicely than a raw Map>

http://commons.apache.org/collections/api-3.1/org/apache/commons/collections/MultiMap.html

http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Multimap.html

Upvotes: 0

yurib
yurib

Reputation: 8147

every user can have more than one book, but when you do:

UserMap.put(1,libraryMap);

you overwrite whatever was in the UserMap under the key 1. so you should make the UserMap of type TreeMap<Integer,ArrayList<whatever>> and add data to the list.

also, there's no sense in nesting the maps. each entity in you program (user/book/library) has an id, so i would suggest keeping a single map for each type of entity and using ids for reference. i.e:

TreeMap<Integer, Integer> bookMap = new TreeMap<Integer, Integer>();    
TreeMap<Integer, ArrayList<Integer>> userMap = new TreeMap<Integer, Integer>();

// for each line
  // read line and assign appropriate values to uid, libid, bid, year

  if (bookMap.get(bid) == null)
    bookMap.put(bid,year);

  if (userMap.get(uid) == null))
    userMap.put(uid, new ArrayList<Integer>());
  userMap.get(uid).add(bid);

now if you want to know which books user 1 has, you do something like:

for (Integer i: userMap.get(1))
  System.out.println(i+","+bookMap.get(i))

Upvotes: 3

Related Questions