Java Maps 数据结构图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3093718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:24:42  来源:igfitidea点击:

Map of Maps data structure

javadata-structurescollections

提问by Dónal

The MultiValueMapclass (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.

MultiValueMap类(Apache的公共集合)可以很容易地工作,一个Map,它的值是集合。我正在寻找一个类,它可以很容易地使用一个 Map,它的键是对象,值是 Map。

I'm using Java 1.4, so can't use Google Collections or generics.

我使用的是 Java 1.4,所以不能使用 Google Collections 或泛型。

回答by Roman

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 patternwhich 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")

回答by Chris Dutrow

The regular Map collection works for this:

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

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

事实上,如果你不担心类型安全,你可以把任何你想要的东西放到 value 部分:

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

回答by Donal Fellows

If you've got a map:{string,map:{string,thing}}(deliberately notusing 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 tuplethat does equals()correctly and hashCode()intelligently) but if you are doing a lot of inserts and deletes then it's less good.

如果您有一个map:{string,map:{string,thing}}(故意使用 Java 语法来避免整个 Java1.4/Java5 业务),那么您还应该考虑是否应该将其建模为map:{tuple:{string,string},thing}. 如果多层次查找占主导地位,那么这是一个很好的变化,使(前提是你实现一个良好的tuple,做equals()正确和hashCode()明智),但如果你正在做大量的插入和删除,然后它的好少。

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

hashCode 中的智能可能意味着只是想出一种合理的方法来将内容的 hashCode 中的位混合在一起。如果期望成员值来自不相交的集合(例如,姓名和职业),那么您可以将它们异或在一起——不完美,但便宜且快速——但是如果你的控制/确定性较少,那么你需要做一些其他的事情很好(例如,在 XOR 之前旋转这些值之一的位)。