地图数据结构的地图
MultiValueMap 类 (Apache commons集合)使得使用值为集合的映射变得容易。我正在寻找一个类,可以轻松使用键是对象、值是映射的映射。
我使用的是 Java 1.4,因此无法使用 Google Collections 或泛型。
The MultiValueMap class (Apache commons collections) makes it easy to work with a Map whose values are Collections. I'm looking for a a class that makes it easy to work with a Map whose keys are objects and values are Maps.
I'm using Java 1.4, so can't use Google Collections or generics.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
地图的地图实际上是一个没有单个根节点的树型结构(以及地图的地图的地图......)。
您可以查看复合模式,它广泛用于实现树结构(如果它们的组件具有相同的类型,但我感觉情况并非如此)。
另一个解决方案是实现一个简单的域模型。它会更清晰易读并且易于维护,例如
:
Map of maps is actually a tree-type structure without single root node (as well as map of maps of maps...).
You can look at Composite pattern which is widely used for implementing tree structures (if their components has the same type which is not the case as I feel).
Another solution is to implement a simple domain model. It'll be much clearer to read and easy to maintain something like:
than
常规的 Map 集合适用于此:
事实上,如果您不担心类型安全,您可以将任何您想要的内容放入值部分:
The regular Map collection works for this:
In fact, if you're not worried about type safety, you can put whatever you want into the value section:
如果您有一个
map:{string,map:{string,thing}}
(故意不使用 Java 语法来避免整个 Java1.4/Java5 业务)那么您还应该考虑是否应该将其建模为map:{tuple:{string,string},thing}
。如果多级查找占主导地位,那么这是一个很好的改变(前提是您实现了一个好的tuple
,可以正确执行equals()
和hashCode() 智能地),但是如果您进行大量插入和删除操作,那么效果就不那么好了。
hashCode 中的智能可能意味着想出一种合理的方法将内容的 hashCode 中的位混合在一起。如果成员值预计来自不相交的集合(例如,姓名和职业),那么您可以将它们异或在一起 - 不完美,但便宜且快速 - 但如果您的控制/确定性较差,那么您需要做其他事情,例如好(例如,在 XOR 之前旋转其中一个值的位)。
If you've got a
map:{string,map:{string,thing}}
(deliberately not using Java syntax to avoid the whole Java1.4/Java5 business) then you should also consider whether you should instead model that asmap:{tuple:{string,string},thing}
. If multi-level lookups dominate, then that's a good change to make (provided you implement a goodtuple
that doesequals()
correctly andhashCode()
intelligently) but if you are doing a lot of inserts and deletes then it's less good.Intelligence in hashCode probably means just coming up with a reasonable way to mix the bits from the hashCodes of the contents together. If the member values are expected to be from disjoint sets (e.g., names and occupations) then you can just XOR them together – imperfect, but cheap and fast – but if you've less control/certainty then you need to do something else as well (e.g., rotate the bits of one of the values prior to the XOR).