本文介绍了地图在Guava的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码与 Map< String,Map< String,String>> 对象,它工作(它被实例化为HashMap的HashMap)想知道是否有更好的方式来表示这个数据结构在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.

我考虑过 Multimap ,但有 ListMultimap SetMultimap 在Guava,我没有找到MapMultimap。

I have considered Multimap, but while there is ListMultimap and SetMultimap in Guava, I have found no "MapMultimap".

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Table.html\">表格,看起来更像它,但其名称是让我不舒服:我有的绝对不是一张桌子,而是一棵树。 (第二个键之间没有重叠)

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. (There is no overlap between the second keys)

是否有更好的Guava替代品,或者我应该坚持使用 Map< String,Map& String>>

Is there a better Guava alternative or should I stick with Map<String, Map<String, String>>?

推荐答案

>似乎很适合你的需要。但确保你选择正确的实现。特别是,如果你的第二个键是不同的(表中的列),结果表将是稀疏的,你应该考虑到这一点来管理内存使用。

Table seems well suited for your need. 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.

所以你应该避免ArrayTable,但可以使用任何其他实现。请注意,提及了 ImmutableTable 已经针对更稀疏和更密集的数据集优化了实施。

So you should avoid the ArrayTable, but can use any of the other implementations. Note that the docs mention that ImmutableTable has optimized implementations for sparser and denser data sets.

如果 Table 是可以使用,并受益于此优化,以及如果表在多个线程之间共享,简化您的生活。

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.

这篇关于地图在Guava的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 06:13