简体   繁体   English

地图在番石榴地图

[英]Map in Map in Guava

I have some code with Map<String, Map<String, String>> objects, which works (it is instantiated as a HashMap of HashMaps), but I wonder whether there is a better way to represent this data structure in Guava. 我有一些代码与Map<String, Map<String, String>>对象有效(它被实例化为HashMaps的HashMap),但我想知道是否有更好的方法在Guava中表示这个数据结构。

I have considered Multimap , but while there is ListMultimap and SetMultimap in Guava, I have found no "MapMultimap". 我考虑过Multimap ,但是在Guava中有ListMultimapSetMultimap ,我没有找到“MapMultimap”。

I have also checked Table , which seems to be more like it, but its name is making me uncomfortable: what I have is definitely not a table but a tree. 我也检查过Table ,它看起来更像是它,但它的名字让我感到不舒服:我所拥有的绝对不是一张桌子而是一棵树。 (There is no overlap between the second keys) (第二个键之间没有重叠)

Is there a better Guava alternative or should I stick with Map<String, Map<String, String>> ? 是否有更好的番石榴替代品或者我应该坚持使用Map<String, Map<String, String>>

Table seems well suited for your need. Table似乎非常适合您的需要。 But make sure you choose the proper implementation. 但请确保选择正确的实施方案。 In particular, if your second keys are all distinct (the columns in the table) the resulting table will be sparse and you should take that into account to manage memory usage. 特别是,如果您的第二个键都是不同的(表中的列),结果表将是稀疏的,您应该考虑到这一点来管理内存使用情况。

So you should avoid the ArrayTable, but can use any of the other implementations. 所以你应该避免使用ArrayTable,但可以使用任何其他实现。 Note that the docs mention that ImmutableTable has optimized implementations for sparser and denser data sets. 请注意, 文档提到ImmutableTable优化了稀疏和密集数据集的实现。

If your Table is constructed at once, you can use an ImmutableTable.Builder and benefit from this optimisation as well as simplify your life if the table is shared among several threads. 如果您的Table是一次构造的,您可以使用ImmutableTable.Builder并从此优化中受益,并且如果表在多个线程之间共享,则可以简化您的生活。

Take a look at Guava's Table interface. 看看Guava的Table界面。

Its documentation example seems to overlap quite a bit with your use-case: 它的文档示例似乎与您的用例重叠很多:

Typically, when you are trying to index on more than one key at a time, you will wind up with something like Map<FirstName, Map<LastName, Person>>, which is ugly and awkward to use. 通常,当您尝试一次索引多个键时,最终将使用Map <FirstName,Map <LastName,Person >>等内容,这些内容难以使用且难以使用。 Guava provides a new collection type, Table, which supports this use case for any "row" type and "column" type. Guava提供了一种新的集合类型Table,它支持任何“row”类型和“column”类型的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM