初始化TreeMap中的数据

发布于 2024-12-28 20:36:00 字数 2433 浏览 1 评论 0 原文

给定数据,例如: 用户、图书馆 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);
             }
        }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白龙吟 2025-01-04 20:36:00

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

UserMap.put(1,libraryMap);

您将覆盖 UserMap 中键 1 下的所有内容。因此,您应该创建 TreeMap> 类型的 UserMap 并将数据添加到列表中。

另外,嵌套地图是没有意义的。程序中的每个实体(用户/书籍/图书馆)都有一个 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))

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))
我喜欢麦丽素 2025-01-04 20:36:00

如果您可以使用任意代码,请考虑使用 Multimap 集合之一,它比原始 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文