在TreeMap中初始化数据

时间:2012-01-24 18:43:04

标签: java treemap

鉴于以下数据: 用户,图书馆ID,书籍ID,年份

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

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

我尝试使用TreeMap存储它们,但是第二组覆盖了第一组,任何建议?

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

解决方案#1 仍然只使用地图,但我认为这个解决方案需要很长时间并消耗更多内存?

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

2 个答案:

答案 0 :(得分:3)

每个用户都可以拥有多本书,但是当你这样做时:

UserMap.put(1,libraryMap);

您会覆盖密钥UserMap下的1中的内容。因此,您应该创建UserMap类型的TreeMap<Integer,ArrayList<whatever>>并将数据添加到列表中。

另外,嵌套地图没有任何意义。你程序中的每个实体(用户/书/库)都有一个id,所以我建议为每种类型的实体保留一个地图,并使用id作为参考。即:

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

现在,如果你想知道用户1有哪些书籍,你可以这样做:

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

答案 1 :(得分:0)

如果您可以使用任意代码,请考虑使用其中一个Multimap集合,它可以处理您尝试解决的问题,而不是原始Map&gt;

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