初始化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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个用户都可以拥有多本书,但是当您这样做时:
您将覆盖
UserMap
中键1
下的所有内容。因此,您应该创建TreeMap>
类型的UserMap
并将数据添加到列表中。另外,嵌套地图是没有意义的。程序中的每个实体(用户/书籍/图书馆)都有一个 id,因此我建议为每种类型的实体保留一个映射,并使用 id 作为参考。即:
现在如果你想知道用户 1 有哪些书,你可以这样做:
every user can have more than one book, but when you do:
you overwrite whatever was in the
UserMap
under the key1
. so you should make theUserMap
of typeTreeMap<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:
now if you want to know which books user 1 has, you do something like:
如果您可以使用任意代码,请考虑使用 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