地图数据结构的地图

发布于 2024-09-06 22:03:53 字数 279 浏览 2 评论 0原文

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 技术交流群。

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

发布评论

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

评论(3

嗼ふ静 2024-09-13 22:03:54

地图的地图实际上是一个没有单个根节点的树型结构(以及地图的地图的地图......)。

您可以查看复合模式,它广泛用于实现树结构(如果它们的组件具有相同的类型,但我感觉情况并非如此)。

另一个解决方案是实现一个简单的域模型。它会更清晰易读并且易于维护,例如

school.getPupil ("John Doe").getMark ("Math")

school.get ("John Doe").get ("Math")

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:

school.getPupil ("John Doe").getMark ("Math")

than

school.get ("John Doe").get ("Math")
何必那么矫情 2024-09-13 22:03:54

常规的 Map 集合适用于此:

    Map<Object,Map<Object,Object>> mapOfMaps = new LinkedHashMap<Object,Map<Object,Object>>();
    Object newObject = new String("object as string");
    mapOfMaps.put(newObject, new LinkedHashMap<Object,Object>());
    Map<Object,Object> objectMap = mapOfMaps.get(newObject);

事实上,如果您不担心类型安全,您可以将任何您想要的内容放入值部分:

    Map<Object,Object> mapOfWhatever = new LinkedHashMap<Object,Object>();
    Object newObject = new String("object as string");
    mapOfWhatever.put(newObject, new LinkedHashMap<Object,Object>());
    Map<Object,Object> objectMap = (Map<Object, Object>) mapOfWhatever.get(newObject);

The regular Map collection works for this:

    Map<Object,Map<Object,Object>> mapOfMaps = new LinkedHashMap<Object,Map<Object,Object>>();
    Object newObject = new String("object as string");
    mapOfMaps.put(newObject, new LinkedHashMap<Object,Object>());
    Map<Object,Object> objectMap = mapOfMaps.get(newObject);

In fact, if you're not worried about type safety, you can put whatever you want into the value section:

    Map<Object,Object> mapOfWhatever = new LinkedHashMap<Object,Object>();
    Object newObject = new String("object as string");
    mapOfWhatever.put(newObject, new LinkedHashMap<Object,Object>());
    Map<Object,Object> objectMap = (Map<Object, Object>) mapOfWhatever.get(newObject);
风筝在阴天搁浅。 2024-09-13 22:03:54

如果您有一个 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 as map:{tuple:{string,string},thing}. If multi-level lookups dominate, then that's a good change to make (provided you implement a good tuple that does equals() correctly and hashCode() 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).

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